views:

135

answers:

4

Hi there!

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!

+3  A: 

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:

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);
}

var something = new fooObj(x,y,z);
something.otherMethod(x,y,z); <--- draws an object, a canvas, for example.

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.

function Member(n) {
    this.member = n;
};

Member.prototype.memberNumber = function() { alert( this.member ) };

var john = new Member(1);
john.memberNumber()

Or

function Custom() {
    this.member = 2;
};

Object.prototype.alertX = function() {
    alert( this.member )
}

var x = new Custom();
x.alertX()

"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!"

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.member and it will work, if not you did not properly set the member property. Simple as that.

meder
A: 

I believe you would have to declare Object as a function (as meder said) or it won't work correctly...

Eugene Bulkin
+1  A: 

Edit: Updated for your updated code.

Because constructors are just functions and Object(n) { this.member = n } is the same as

Object(n); // this will throw a reference error as n is undefined
{ // this is a code block
  this.member = n; // this will also throw a reference error as n is undefined
}

To make the constructor you want, use this:

var MyConstructor = function (n) {
  this.member = n;
};

Also, Object is already a constructor that all JavaScript objects inherit from, so don't redefine it as something else.

Eli Grey
i used Object as a generic object, could perfectly be objectX or something.
Gabriel A. Zorrilla
A: 

This works:

     function TestObj(n) {
  this.testMember = n;
 }

 TestObj.prototype.alertX = function() {
  alert(this.testMember);
 }

 foo = new TestObj(4);
 foo.alertX();
Gabriel McAdams