javascript原型链 请考虑这样的代码: class a { constructor() { this.test = "test from a"; this.testA = "X"; } mimi() { console.log('aaaaaaaa', this.test, this.testA) } } class b extends a { constructor() { super(); this.test = "test from b" } mimi() { console.log('bbbbbbbb', this.test, this.testA) } } class c extends b { constructor() { super(); this.test = "test from c" } mimi() { console.log('cccccccc', this.test, this.testA) } meme() { var test = kalreg.__proto__.__proto__; test.mimi(); var test2 = kalreg.__proto__.__proto__.__proto__; test2.mimi(); } } var kalreg = new c(); kalreg.mimi(); kalreg.meme();
我得到的结果是:javascript原型链 请考虑这样的代码: class a { constructor() { this.test = "test from a"; this.testA = "X"; } mimi() { console.log('aaaaaaaa', this.test, this.testA) } } class b extends a { constructor() { super(); this.test = "test from b" } mimi() { console.log('bbbbbbbb', this.test, this.testA) } } class c extends b { constructor() { super(); this.test = "test from c" } mimi() { console.log('cccccccc', this.test, this.testA) } meme() { var test = kalreg.__proto__.__proto__; test.mimi(); var test2 = kalreg.__proto__.__proto__.__proto__; test2.mimi(); } } var kalreg = new c(); kalreg.mimi(); kalreg.meme();,javascript,prototype,chain,Javascript,Prototype,Chain,我得到的结果是: cccccccc test from c X bbbbbbbb undefined undefined aaaaaaaa undefined undefined 我的对象逻辑使我使用“a”作为最通用的类,“b”是它的子类,“c”是“b”的子类。我希望“c”拥有“b”和“a”的所有方法和属性,但“a”的部分功能被“b”覆盖,因此“c”访问被覆盖功能的唯一方法是使用原型链。 不幸的是,我的方法行不通,所以问题是: 在meme() 在我看来,输出中不应该有“未定义的”,然而,有。
cccccccc test from c X
bbbbbbbb undefined undefined
aaaaaaaa undefined undefined
我的对象逻辑使我使用“a”作为最通用的类,“b”是它的子类,“c”是“b”的子类。我希望“c”拥有“b”和“a”的所有方法和属性,但“a”的部分功能被“b”覆盖,因此“c”访问被覆盖功能的唯一方法是使用原型链。
不幸的是,我的方法行不通,所以问题是:
meme()
meme()函数中,如何避免kalreg.proto
对于确实需要从超类访问属性的用例,请使用super
(但请注意,这对test
和testA
没有帮助,因为它们不在超类或其prototype
对象上,它们在使用new c
创建的实例上;下面有更多信息)
分别:避免\uuuuuuuuuuuuuu
。在少数需要访问对象原型的情况下,请使用object.getPrototypeOf
在我看来,不应该有“未定义”的输出,但有。预期产出为:
来自Cx的CCCC测试
来自Bx的BBBB测试
aaaaaaaaaaaax测试
输出是正确的,因为您调用的mimi
的原型对象没有test
或testA
属性。只有使用new c
创建的对象才具有这些属性。而且只有一个这样的对象,所以无论调用哪个mimi
,test
都将是“从c进行测试”
,而testA
将始终是“X”
在一篇评论中,您询问了当每个构造函数中都有this.test=…
时,为什么只能有一个test
。在完成var kalreg=new c()之后,让我们看看内存中的内容代码>:
如果每个类的构造函数中都定义了this.test,那么对象怎么可能没有this.test呢?你能详细说明一下如何使用super
来实现meme()
方法中的OPs目标吗。@Adam:完成了,谢谢你把它标记出来,我本想回到上面,但忘了。:-)@我已经添加了图片和更多的解释。
+−−−−−−−−−−−−+
a−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+−>| (function) | +−−−>(Function.prototype)
| +−−−−−−−−−−−−+ |
| | __proto__ |−−+ +−−−−−−−−−−−−−+
| | prototype |−−−−−−−−−−+−>| (object) |
| +−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+
| | | __proto__ |−−>...
+−−−−−−−−−−−−+ | | | constructor |−−>a
b−−−−−−−−−−−−−−−−−−−+−>| (function) | | | | mimi |−−>...
| +−−−−−−−−−−−−+ | | +−−−−−−−−−−−−−+
| | __proto__ |−+ +−−−−−−−−−−−−−+ |
| | prototype |−−−−−−−−+−>| (object) | |
| +−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+ |
| | | __proto__ |−−+
+−−−−−−−−−−−−+ | | | constructor |−−>b
c−−−>| (function) | | | | mimi |−−>...
+−−−−−−−−−−−−+ | | +−−−−−−−−−−−−−+
| __proto__ |−+ +−−−−−−−−−−−−−+ |
| prototype |−−−−−−−+−>| (object) | |
+−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+ |
| | __proto__ |−+
| | constructor |−−>c
+−−−−−−−−−−−−+ | | mimi |−−>...
kalreg−−−>| (object) | | | meme |−−>...
+−−−−−−−−−−−−+ | +−−−−−−−−−−−−−+
| __proto__ |−−+
| test |
| testA |
+−−−−−−−−−−−−+