Why if i try:
Object(n) //Constructor
{
this.member = n;
}
Object.prototype.alertX = function()// Method
{
alert(this.member);
}
foo = new Object(4);
Object.alertX;
I get “this. member is not defined”.
How can i access the constructor’s member inside one of its methods?
EDITED: my original purpose is to have an internal method access the already created object member, not to access the object’s method from creating another object, the object is already created!
Thanks!
EDITED 2:
Tried this:
var fooObj = function(x,y,z){ // Map object constuctor.
this.x = x;
this.y = y;
this.z = y;
}
fooObj.prototype.test = function(){
alert(this.x);
}
***initialization****
something = new fooObj();
something.otherMethod(x,y,z); <--- draws an object, a canvas, for example.
document.getElementById('canvas').addEventListener('mouseup', something.test, false);
When i press over the object, should fire of the alert, but get the this.x not defined. Do i have to give it a value? The object was already created and executed one of its functions!
Edited again…
Your constructor accepts 3 arguments and sets them on the object. You are not feeding them in the initial invocation. Now, assuming you did not leave even more information out, change it to:
Otherwise if you are leaving something out, please paste an ENTIRE replica of your code to not waste time.
Again, to re-iterate, make sure the properties are being set. Otherwise the explanation is that they’re not being set, simple explanation.
Or
Whatever constructor you used or however you set the property, make sure it was set. Then just attach a method to the prototype alerting
this.memberand it will work, if not you did not properly set the member property. Simple as that.