Your phrasing is a bit confusing, but I'll do my best. First, you should note that your curly braces are a bit off. Your code should look like this:
function ObjectCreate(){
this.a = "a";
this.b = "b";
}
ObjectCreate.prototype.show = function(){
alert(this.a+" "+this.b);
}
obj1 = new ObjectCreate();
You need to define your constructor, then attach things to its prototype.
When you call the constructor, the this
keyword does basically refer to the new object being created. This is important because, for example, you could write a constructor like:
function ObjectCreate(x,y){
this.a = x*x;
this.b = y*x+4;
}
obj1 = new ObjectCreate(10,20);
Here, as in all constructors, this
refers to the instance being created (obj1
, in this case).
I think you suggested that this
refers to the object's prototype, but that would make this.a
and this.b
static variables, which would be useless in a constructor like the one here, since every time you initialized a new object you would change the vale of this.a
and this.b
for all previously existing objects, and that just isn't useful.
I hope that answered your question. If not, please go ahead and leave comments for further clarification.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…