function Foo() {
this.name="2";
}
Foo.prototype.method = function() {
console.log(this.name);//1
console.log(this);// Foo { name="1", method=function()}
};
function Bar() {
}
Bar.prototype = Foo.prototype;
var a = new Bar();
a.name = "1";
a.method();
哪位能指点一下这段代码,为什么输出this指向Foo,同时为什么this也有了个name=“1”的属性
巴扎黑2017-04-10 15:05:59
Bar
而不是Foo
。Bar
构造函数添加了一个属性name = 1
。