How to access somevar:'this is Foo' from inside bar.foo() ?
function Foo(){
this.somevar='this is Foo';
}
Foo.prototype={
bar:{
somevar:'this is bar'
,foo:function(){
console.log(this);
}
}
}
var instance = new Foo();
instance.bar.foo();
Here is link to jsfiddle:
http://jsfiddle.net/jct8n/3/
I think perhaps the scoping rules of the language will not allow this in any straightforward way. The best suggestion I can come up with to get what you want (short of renaming
somevarto get rid of the clash in the first place) is to useapply()to change the value ofthisinside offoo(), like:Here’s a fiddle:
http://jsfiddle.net/jct8n/2/