If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

If you want to add new properties/methods to a Constructor, you must add them to its prototype, so that any other object which might be created using that Constructor could share the new property/method. That is the principle of delegation in JavaScript. What is not clear so far?

Code:

function Constructor(name,color){ // this is a constructor
this.name=name;
this.color=color;
}
Constructor.prototype.alertColor=function(){ // this is a new method added straight to the Constructor via its prototype; it will work for all the objects which will be created using this constructor
alert(this.color);
}
var myObject=new Constructor('myObject','red'); // this is an object
myObject.alertName=function(){alert(this.name)} // this is new method added to the object; it will work only for this particular object
//test
myObject.alertName(); // has its own method
myObject.alertColor(); // gets the general method of the prototype of the constructor
var newObject=new Constructor('newObject','green'); // this is another object created using the same constructor
//test
newObject.alertColor(); // gets the general method of the prototype of the constructor (by delegation)

Anything to do with the Creation of the Object must happen in the constructor or the prototype. End of Story. Any thing created out side the constructor but on the same instance is irrelevant.

i.e.

Person.getMyOtherStuff = new function () {

return this.ctx;

};

Not quite. The Constructor (in your case, the function Person()) can be treated also like an object, as any other function, but as an object with its own properties, which have nothing in common with the Object constructed (your case: p)

You should, also, understand the position and the meaning of the token this

Here:

Code:

var Person = function () {
this.ctx = 'this is some other stuff';
}

thisdoes not refer the constructor Person() itself. It refers the object which might be created via that constructor. For instance, your case, the object p.

On the contrary, in the code:

Code:

Person.getMyOtherStuff = function () {
return this.ctx;
}

this refers the function Person(), not the object created via that function. Or the function Person() has no property called ctx, thus there will be an error.