0
當我檢查instanceof
方法時,結果不一樣。javascript原型構造函數和instanceof
function A(){}
function B(){};
首先我分配prototype
(參考)屬性,進A
A.prototype = B.prototype;
var carA = new A();
console.log(B.prototype.constructor);
console.log(A.prototype.constructor == B);
console.log(B.prototype.constructor == B);
console.log(carA instanceof A);
console.log(carA instanceof B);
最後4條件對上述返回true
。
但是,當我試圖指定B的constructor
..結果不一樣。
A.prototype.constructor = B.prototype.constructor;
var carA = new A();
console.log(B.prototype.constructor);
console.log(A.prototype.constructor == B);
console.log(B.prototype.constructor == B);
console.log(carA instanceof A);
console.log(carA instanceof B);
在這種情況下carA instanceof B
返回false
。爲什麼它返回false