【发布时间】:2016-08-19 01:57:15
【问题描述】:
function test() {
this.a = {
b: 4
};
}
test.prototype.a = {
c: 5
};
var example = new test();
为什么是example.a.c == undefined?
难道不应该继承原型并返回5吗?
如果这不可能,有没有办法添加代码来返回原型?:
function test() {
this.a = {
b: 4,
c: *this.return.prototype*
};
}
【问题讨论】:
-
您的原型适用于
example.a.c,但this.a优先于test.prototype.a。 -
@Barmar 所以它不像 $.extend() 有 2 个对象创建一个新对象 a = {b: 4, c: 5};?
-
没错,原型不是递归合并的。
-
您可以为
a.c定义一个返回this.prototype.a.c的getter。 -
@Barmar 这样的电话会是什么样子?
标签: javascript object prototype