If I have The code:
function RandomObjectThatIsntNamedObjectWhichIOriginallyNamedObjectOnAccident() {
this.foo = 0;
this.bar = function () {
this.naba = function () {
//How do I reference foo here?
}
}
}
You need a self reference:
function RandomObjectThatIsntNamedObjectWhichIOriginallyNamedObjectOnAccident() {
var self = this;
this.foo = 0;
this.bar = function () {
this.naba = function () {
self.foo; //foo!
}
}
}
ObjectTry the following
function SomeObject() {
var self = this;
this.foo = 0;
this.bar = function () {
this.naba = function () {
//How do I reference foo here?
self.foo
}
}
}
var self, because this.self will make self publicly accessible.First: Don't name your function Object, it will shadow the global Object constructor.
I don't see a reason why you have to assign naba inside bar to the instance. Why are you doing this? You can assign both bar and naba to the functions prototype:
function MyConstructor() {
this.foo = 0;
}
MyConstructor.prototype.bar = function () {
};
MyConstructor.prototype.naba = function () {
// this.foo
};
Eventually it depends on how you are calling the naba function. The fact that you are assigning it to this suggests you want to call it with
var obj = new MyConstructor();
obj.naba();
If you want to add naba only after bar was called, you can still access foo via this.foo:
MyConstructor.prototype.bar = function () {
if(!this.naba) {
this.naba = function() {
// this.foo
};
}
};
var obj = new MyConstructor();
obj.bar();
obj.naba();
If you want a proper/better answer, you have to show how you are going to use naba.
naba gets assigned to bar, not the object. I general I would suggest you don't use this or prototype at all because it's very error prone. Most people seem to completely misunderstand how JavaScript works (and that it works without prototype). If you'd like I could post an equivalent solution that doesn't use this or prototype.naba never gets assigned to bar, it gets assigned to whatever this refers to. Most likely it is an instance of the constructor function, but it could as well be window. But this is never bar. And if you have a constructor function, then using a prototype is the preferred way to attach instance methods (saves memory). And I don't see what is error prone with this or prototype. It might be difficult to understand at first, but there is nothing overall mystical about it.this is confusing. Here's an example: jsfiddle.net/JZ7c2this refers to depends on how the function is called. There are four different ways, see my answer here. Which means that in order to give a proper answer, we would have to know how the OP is calling all these functions.Interestingly enough, you don't need to do anything special.
The this reference works:
function SomeObject() {
this.foo = 0;
this.bar = function () {
this.naba = function () {
alert(this.foo); // this works!
}
}
}
Since you're assigning "methods" always to the same reference, this will still point to it inside bar, and later inside naba.
Object is a function and NOT an object. try it with new. then it does not work.var obj = new Object(); obj.bar(); obj.naba(); then it will work, because this always references obj.
naba. How are you calling it? What are you doing withObject? Whatthisrefers to eventually depends on how you are calling the functions. As long as we don't know this, we can only guess what would be the best solution.