I'm trying to put some structure to my JavaScript code and want to add some prototype functions to a namespace, like this:
Person.drink();
Person.eat.steak();
I've tried something like this, but it doesn't work as expected:
var Person = function() {
this.thirsty = true;
this.hungry = true;
this.drink();
this.eat.steak();
}
Person.prototype.drink = function() {
this.thirsty = false;
}
Person.prototype.eat = {
steak: function() {
this.hungry = false;
}
};
var me = new Person();
console.log(me.thirsty, me.hungry);
Console output: false, true (Fiddle: http://jsfiddle.net/upwz4u32/2/)
Why does my nested/namespaced function doesn't have access to it's instance variables?
thisin the second context isme.eatjsfiddle.net/upwz4u32/3. try to usecall/applylike this: jsfiddle.net/upwz4u32/4Personis a class, not namespace,