
class A { } class B extends A{ } let b = new B() console.log( b.__proto__) 打印结果是A{}, 不是很能理解它的含义是什么, b.__proto___不是等于 B.prototype 吗
1 USDT 2024-09-03 15:41:15 +08:00 对呀。。B.prototype 就是 A 啊,你 console.log 一下 B.prototype 就知道了 |
2 Suaxi 2024-09-03 15:49:00 +08:00 |
3 zephyru 2024-09-03 16:05:01 +08:00 prototype 不就是像上找一层么?还有什么别的解释?你 new 个 A 试试,在拿 prototype 估计就是空对象了 __proto___ 这个也是 |
4 newaccount 2024-09-03 16:07:54 +08:00 你看看 firefox 下是不是你认为的结果 |
5 rabbbit 2024-09-03 16:09:20 +08:00 B.prototype.__proto__ === A.prototype |
6 daolanfler 2024-09-03 16:37:34 +08:00 B.prototype instanceof A // true B.prototype.__proto__ === A.prototype // true A.prototype instanceof Object // true const a = new A() chrome dev console 里面也是 a 也是 A{} 这种形式展示的 |
7 rookiemaster OP 谢谢各位,清晰了 |
8 lyxxxh2 2024-09-03 17:03:13 +08:00 刚解决了个 bug 。 基类: def init(): self.cOnfig= Config().config self.config.update(self.get_config()) 多个类继承基,a 类总是被 b 类改变。 debug 半天找不到问题。 又怀疑基类变量的共享,但变量也不在基类啊。 最后想起... Config()特码的是单例啊。 |
9 mizuki9 2024-09-03 19:34:25 +08:00 via Android 运行了一下,打印出来不是 B{}吗? |
10 AV1 2024-09-03 19:43:00 +08:00 对的没错,b.__proto___ 和 B.prototype 都是 A 的实例。 Object.getPrototypeOf(b) === B.prototype > true B.prototype instanceof A > true |
11 guiyumin 2024-09-04 10:36:00 +08:00 恕我直言,这有意义吗 |