unable to call a function defined by an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawnmoth

    #1

    unable to call a function defined by an object

    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.change Name("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?
  • taps128

    #2
    Re: unable to call a function defined by an object

    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.change Name("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();

    Comment

    • RobG

      #3
      Re: unable to call a function defined by an object



      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.change Name("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

      Comment

      Working...