Have a look on my code. I don't understand how It's working
function doSomething() {
this.testProp = 'testProp';
}
doSomething.testProp1 = "testProp1";
var doSomeInstancing = new doSomething();
console.log("doSomething.testProp:" + doSomething.testProp);
//undefined
console.log("doSomething.testProp1:" + doSomething.testProp1);
//testProp1
console.log(doSomething.hasOwnProperty('testProp1'));
//true
console.log("doSomeInstancing.testProp:" + doSomeInstancing.testProp);
//testProp
console.log("doSomeInstancing.testProp1:" + doSomeInstancing.testProp1);
//undefined
console.log(doSomeInstancing.hasOwnProperty('testProp1'));
//false
The question is why testProp is undefined in doSomething and not in doSomeInstancing and vice versa for testProp1.
You are dealing with two distinct objects here:
the function:
doSomething
the object:doSomeInstancing
Both are objects and both can have properties.
doSomeInstancing
is the object returned from the function when you callnew doSomething()
and it is whatthis
refers to in the body of the function. Adding properties to this object has no effect on the other object, thedoSomething
function. The opposite is also true.If you are trying to inherit a property, you are actually looking for a third object:
doSomething.prototype
. This is a property of functions that points to an object that instances will be linked to.doSomeInstancing
will inherit from this object to in the prototype chain. For example: