Methord in Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • binnyva@hotmail.com

    #1

    Methord in Class

    Hello Everyone,

    How can I add a methord to a class? Consider the code...

    ------------

    function makeMovie(name, lead)
    {
    this.title = name
    this.actors = lead
    function showLead()
    {
    alert("Lead Actors are "+this.acto rs)
    }
    }

    var film = new makeMovie("What ever","Whoever" )
    film.showLead()

    -----------

    This gives an error
    "Object don't support this property or methord."
    Thanking You in advance,
    Binny V A
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


  • Martin Honnen

    #2
    Re: Methord in Class



    binnyva@hotmail .com wrote:

    [color=blue]
    > How can I add a methord to a class? Consider the code...[/color]
    [color=blue]
    > function makeMovie(name, lead)
    > {
    > this.title = name
    > this.actors = lead
    > function showLead()
    > {
    > alert("Lead Actors are "+this.acto rs)
    > }
    > }[/color]

    Use
    makeMovie.proto type.showLead = function () {
    alert("Lead Actors are " + this.actors);
    };
    [color=blue]
    > var film = new makeMovie("What ever","Whoever" )
    > film.showLead()[/color]


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Douglas Crockford

      #3
      Re: Methord in Class

      > How can I add a methord to a class? Consider the code...[color=blue]
      >
      > ------------
      >
      > function makeMovie(name, lead)
      > {
      > this.title = name
      > this.actors = lead
      > function showLead()
      > {
      > alert("Lead Actors are "+this.acto rs)
      > }
      > }
      >
      > var film = new makeMovie("What ever","Whoever" )
      > film.showLead()
      >
      > -----------
      >
      > This gives an error[/color]

      You have two choices:

      As you wrote it, showLoad is a private method. This is how you can turn
      showLead into a public method:

      function makeMovie(name, lead) {
      this.title = name;
      this.actors = lead;
      }
      makeMovie.proto type.showLead = function () {
      alert("Lead Actors are " + this.actors)
      };

      This is how you can turn it into a priviledged method:

      function makeMovie(name, lead) {
      this.title = name;
      this.showLead = function () {
      alert("Lead Actors are " + lead)
      }
      }

      Notice that lead is a private member. See

      Comment

      Working...