// object constructor
function ObjectConstructor(a, b, c) {
this.A = a;
this.B = b;
this.C = c;
this.Total = a + b + c;
}
var obj = ObjectConstructor(1, 2, 3);
alert(obj.Total);
If you said the objTotal is 6, then you are wrong? So, what went wrong? The obj has nvr been instantiated. A "new" is omitted in this case.
Do like this to prevent this to be happened.
// object constructor
function ObjectConstructor(a, b, c) {
if (!(this instanceof arguments.callee)) {
return new ObjectConstructor(a, b, c);
}
this.A = a;
this.B = b;
this.C = c;
this.Total = a + b + c;
}
No comments:
Post a Comment