A function’s prototype object has a constructor property pointing back to that function. Instances can access this reference through the prototype chain, use it to check an apparent origin, or call it to create another instance. section 7.2.3 section 7.5
Listing 7.5. Examining the type of an instance and its constructor
function Ninja(){}
const ninja = new Ninja();
assert(typeof ninja === "object", #A
"The type of the instance is object.");
assert(ninja instanceof Ninja, #B
"instanceof identifies the constructor." );
assert(ninja.constructor === Ninja, #C
"The ninja object was created by the Ninja function.");
Compare typeof, instanceof, and constructor to see how each check describes a Ninja instance. section 7.2.3
Watch for a broken back-reference when replacing a constructor’s prototype:
- If Ninja.prototype is replaced with a Person instance, lookup can reach Person’s constructor property, so ninja.constructor identifies Person instead of Ninja. section 7.3.1
- Restore the link with Object.defineProperty, setting constructor to Ninja and making the property non-enumerable. section 7.3.1
Construct another instance
After creating ninja with new Ninja(), new ninja.constructor() creates a distinct Ninja instance, even when the original constructor is no longer in scope. section 7.2.3
