unable to call a function defined by an object

Discussion in 'Javascript' started by yawnmoth, Dec 3, 2007.

  1. yawnmoth

    yawnmoth Guest

    Say I have the following:

    <script>
    var cat = (function() {
    var name = "";

    function changeName(name) {
    this.name = name;
    }

    function talk() {
    alert( this.name + " say meeow!" );
    }

    return function(name) {
    this.name = name;
    talk();
    }
    })();

    firstCat = new cat("pursur");
    //firstCat.changeName("Bill");
    //firstCat.talk(); //alerts "Bill says meeow!"
    </script>

    Why can I call the talk() function, as defined in the cat variable /
    function, from the return function (eg. privately) but not publically?

    Also, setting this.name to name doesn't seem to accomplish anything.
    What shared variable could I set for talk() to print out the same
    value that the return function set?
    yawnmoth, Dec 3, 2007
    #1
    1. Advertising

  2. yawnmoth

    taps128 Guest

    yawnmoth wrote:
    > Say I have the following:
    >
    > <script>
    > var cat = (function() {
    > var name = "";
    >
    > function changeName(name) {
    > this.name = name;
    > }
    >
    > function talk() {
    > alert( this.name + " say meeow!" );
    > }
    >
    > return function(name) {
    > this.name = name;
    > talk();
    > }
    > })();
    >
    > firstCat = new cat("pursur");
    > //firstCat.changeName("Bill");
    > //firstCat.talk(); //alerts "Bill says meeow!"
    > </script>
    >
    > Why can I call the talk() function, as defined in the cat variable /
    > function, from the return function (eg. privately) but not publically?
    >
    > Also, setting this.name to name doesn't seem to accomplish anything.
    > What shared variable could I set for talk() to print out the same
    > value that the return function set?


    Why don't you try
    function Cat(){
    ....
    }

    var firstCat=new Cat();
    taps128, Dec 3, 2007
    #2
    1. Advertising

  3. yawnmoth

    RobG Guest

    yawnmoth wrote:
    > Say I have the following:
    >
    > <script>
    > var cat = (function() {
    > var name = "";
    >
    > function changeName(name) {
    > this.name = name;
    > }
    >
    > function talk() {
    > alert( this.name + " say meeow!" );
    > }
    >
    > return function(name) {
    > this.name = name;
    > talk();
    > }
    > })();
    >
    > firstCat = new cat("pursur");
    > //firstCat.changeName("Bill");
    > //firstCat.talk(); //alerts "Bill says meeow!"
    > </script>
    >
    > Why can I call the talk() function, as defined in the cat variable /
    > function, from the return function (eg. privately) but not publically?
    >
    > Also, setting this.name to name doesn't seem to accomplish anything.
    > What shared variable could I set for talk() to print out the same
    > value that the return function set?


    I think the simplest answer is to ask you to read about closures:

    <URL: http://www.jibbering.com/faq/faq_notes/closures.html >

    You may also like this article on implementing public and private
    members in javascript:

    <URL: http://www.crockford.com/javascript/private.html >

    --
    Rob
    RobG, Dec 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,746
    Keith Thompson
    Sep 27, 2005
  2. grbgooglefan
    Replies:
    2
    Views:
    408
    Pascal Bourguignon
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    4
    Views:
    428
    Kenny McCormack
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    237
  5. Paul Brannan
    Replies:
    13
    Views:
    282
    Paul Brannan
    Mar 4, 2008
Loading...

Share This Page