在JS中,为什么原型属性上的get / set行为是不同的?

时间:2013-08-04 14:30:41

标签: javascript inheritance properties prototype

派生对象可以从其原型对象中获取属性值; 但是当设置该属性时,它将在派生对象中添加新属性。 更多内容见下面的代码。

function Base(name) {this.name  = name };

function Deriver(name) {this.dname  = name };
var base = new Base('base');
Deriver.prototype = base  ;


d1 = new Deriver('d1');
console.log(d1.name);  // it will show "base". That'ok.



d1.name = "new name"; // !!! it does not update the property from its protoype object
                      // it add new property "name" in the object d1. 
console.log(d1.name); // it will show "new name" 
console.log(base.name); // it still show "base"

0 个答案:

没有答案