以下代码执行后,a.x 和 b.x 的结果分别是?
function A(x){
this.x = x;
}
A.prototype.x = 1;
function B(x){
this.x = x;
}
B.prototype = new A();
var a = new A(2), b = new B(3);
delete b.x;
a不用说,主要看b
function A(x){ this.x = x; } A.prototype.x = 1; function B(x){ this.x = x; } B.prototype = new A();//#1 var b = new B(3);//#2 delete b.x;//#3
#1:创建一个 A 的实例对象赋值给 B.prototype,此时 B.prototype = { x : undefined }
#2:创建一个 B 的实例 b,此时 b = { x : 3 },b.__proto__ = B.prototype = { x : undefined }
#3:删除 b 中的 x,此时 b = { },b.__proto__ = B.prototype = { x : undefined }
ref > MDN继承与原型链
题目来源牛客网