The instanceof operator returns false on a subsequent change in the inheritance chain

When the prototype is installed in the constructor function, the operator instanceofreturns trueonly until the prototype is modified. Why?

function SomeConstructorFunction() {
}

function extendAndInstantiate(constructorFn) {
    constructorFn.prototype = {}; //Can be any prototype
    return new constructorFn();
}

var child1 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //true

var child2 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //false
console.log(child2 instanceof SomeConstructorFunction); //true
+5
source share
4 answers

Prototypal inheritance may be a bit of a bend in the brain. I already wrote a simple explanation for prototype inheritance in the following answer, and I suggest you read it: fooobar.com/questions/85508 / ...

Now to answer your question. Consider the following function:

function F() {}

I can create an instance Fusing the newfollowing:

var f = new F;

f instanceof F true. , instanceof F.prototype F true, . . , .

, G F.prototype G.prototype:

function G() {}

F.prototype = G.prototype;

f instanceof F , false. , F.prototype F (, F.prototype G.prototype).

F:

var g = new F;

g instanceof G, true, g = new F. , G.prototype G.

. JavaScript. : JavaScript

+3
constructorFn.prototype = {}; //Can be any prototype

!

constructorFn.prototype = Object.prototype;

.

, extendAndInstantiate() SomeConstructorFunction - ( {}). child1 instanceOf true , SomeConstructorFunction - {}. extendAnInstantiate() , SomeConstructorFunction {}, child2 Of, {}, child1 - Of {}, child1 false, child2 true. , Object.prototype Array.prototype, true.

: {} obj.

var obj = {};
function extendAndInstantiate(constructorFn) {
    constructorFn.prototype = obj;
    return new constructorFn();
}

true, {} .

+3

instanceof .

function extendAndInstantiate(constructorFn) {
constructorFn.prototype = {}; //Can be any prototype
...

{}, . , , instanceof false.

, , true , :

function SomeConstructorFunction() {
}

var emptyObj = {};

function extendAndInstantiate(constructorFn) {
    constructorFn.prototype = emptyObj; 
    return new constructorFn();
}

var child1 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //true

var child2 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //true
console.log(child2 instanceof SomeConstructorFunction); //true
0

, extendAndInstantiate, {}, SomeConstructorFunction. SomeConstructorFunction. , SomeConstructorFunction.

function SomeConstructorFunction() {

}

function extendAndInstantiate(constructorFn) {
  constructorFn.prototype = {}; //New instance as prototype
    return new constructorFn();
}

var child1 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //true

var child2 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //false
console.log(child2 instanceof SomeConstructorFunction); //true


var child3 = extendAndInstantiate(SomeConstructorFunction);
console.log(child1 instanceof SomeConstructorFunction); //false
console.log(child2 instanceof SomeConstructorFunction); //false
console.log(child3 instanceof SomeConstructorFunction); //true

So, either you can follow the method shown by @ ben336, or do a status check before assigning the prototype, as shown below.

function Base(){

}

function SomeConstructorFunction() {

}

function extendAndInstantiate(constructorFn, Base) {
  if(!(constructorFn.prototype instanceof Base)){
    console.log(" extend only once ");
    constructorFn.prototype = new Base(); 
  }
  return new constructorFn();
}


var child1 = extendAndInstantiate(SomeConstructorFunction, Base);
console.log(child1 instanceof SomeConstructorFunction); //true

var child2 = extendAndInstantiate(SomeConstructorFunction, Base);
console.log(child1 instanceof SomeConstructorFunction); //true
console.log(child2 instanceof SomeConstructorFunction); //true


var child3 = extendAndInstantiate(SomeConstructorFunction, Base);
console.log(child1 instanceof SomeConstructorFunction); //true
console.log(child2 instanceof SomeConstructorFunction); //true
console.log(child3 instanceof SomeConstructorFunction); //true
0
source

All Articles