E
Eleandor
Hi, I'm currently facing an annoying problem with the this "pointer".
Here's an abstract version of the code I'm trying to write:
var Apple1 = null;
var Apple2 = new Apple();
function Apple() {
Apple1 = this;
this.eat = function() {
alert("Yummy!");
}
}
The problem I have is that Apple1 remains undefined. What I was
expecting is that "this" would point to the new Apple but apparently
it doesn't. As a result, Apple1 = undefined, and is not equal to
Apple2.
Right now the workaround I have is something like this:
var Apple1 = new Apple(); Apple1.init(Apple1); // where init simply
sets Apple1.self = the parameter passed to init. Clearly this is not a
good solution.
How can I solve this? I don't think it'll be easy to really avoid this.
Here's an abstract version of the code I'm trying to write:
var Apple1 = null;
var Apple2 = new Apple();
function Apple() {
Apple1 = this;
this.eat = function() {
alert("Yummy!");
}
}
The problem I have is that Apple1 remains undefined. What I was
expecting is that "this" would point to the new Apple but apparently
it doesn't. As a result, Apple1 = undefined, and is not equal to
Apple2.
Right now the workaround I have is something like this:
var Apple1 = new Apple(); Apple1.init(Apple1); // where init simply
sets Apple1.self = the parameter passed to init. Clearly this is not a
good solution.
How can I solve this? I don't think it'll be easy to really avoid this.