0

I know that there are lots of topics about how does the private/public methods work in JS, but none of them resolved the issue I'm currently experiencing.

As you can see below, I'm just trying to access a public method from a private method.

    function Animal(name) {
      this.name = name
    }

    Animal.prototype = (function() 
    {
      var sitdown = function() {
        console.log(this.name + ' sits down.');
        standup();
      };
       return {
        standup: function()
        {
           console.log(this.name+' stands up');
           sitdown();
        }
       }
    })();

var Tiger = new Animal("Tiger");
Tiger.standup();

Everything works until it gets to the standup() method.

can you please advise as how can I solve this?

Thanks, Alex

Wracker
  • 577
  • 10
  • 32

2 Answers2

1

you don't define function standup in scope with var sit you need change your code like

Animal.prototype = (function() 
{
    var sit = function() {
        console.log(this.name + ' sits down.');
        standup();
    };
    function standup()
    {
       console.log(this.name+' stands up'); //`this` here is global object, not your created
    }
    return {
        standup: standup
    }
})();

UPDATE: after update OP methinks you need something like this

Animal.prototype = (function() 
{
    var sitdown = function() {
                      console.log(this.name + ' sits down.');
                      standup.call(this);
                  },
        standup = function (){
                      console.log(this.name+' stands up');
                  }
    return {
    standup: function()
    {
       sitdown.call(this);
    }
   }
})();

also more about this keyword

Community
  • 1
  • 1
Grundy
  • 13,060
  • 3
  • 33
  • 51
  • I think you're right, I didn't know that I need to declare the function within the object. Now it all works as I wanted. Thanks – Wracker Oct 08 '14 at 09:04
0

remove return, and all should work fine.

function Animal(name) {
  this.name = name
}

Animal.prototype = (function() 
{
  var sit = function() {
    console.log(this.name + ' sits down.');
    standup();
  };
  var standup = function()
    {
       console.log(this.name+' stands up');
    };
})();
Supamiu
  • 7,767
  • 6
  • 36
  • 72
  • are you try it? possibly you mean `standup= function()` instead `standup: function()`? – Grundy Oct 08 '14 at 08:49
  • oh yes my bad, forgot to edit this part too ^^, i just removed return sentence – Supamiu Oct 08 '14 at 08:55
  • sorry guys, I must have confused you ... I need the standup method to be publicly available.. And once the standup(() is called, it will then initiate the private method sitdown() and then it'll again call standup but from the private method sitdown(). – Wracker Oct 08 '14 at 08:59
  • just change `var standup` for `this.standup` – Supamiu Oct 08 '14 at 09:02
  • That's just part of what needs to be done in this case, if I remove the return part and not add the ` return { standup: standup }` it will not let me access the standup() method after instantiating the object. – Wracker Oct 08 '14 at 09:10