>

有人能帮忙解释一下这段 js 的打印结果吗,问了几次 gpt 都是错误的,

user3 • 9 次点击
class A {
}

class B extends A{
}

let b = new B()
console.log( b.proto)

打印结果是A{}, 不是很能理解它的含义是什么, b.__proto___不是等于 B.prototype 吗

user1

对呀。。B.prototype 就是 A 啊,你 console.log 一下 B.prototype 就知道了

user2
user3

prototype 不就是像上找一层么?还有什么别的解释?你 new 个 A 试试,在拿 prototype 估计就是空对象了 proto_ 这个也是

user1

你看看 firefox 下是不是你认为的结果

user2

B.prototype.proto === A.prototype

user3

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{} 这种形式展示的

user1

谢谢各位,清晰了

user2

刚解决了个 bug 。
基类:
def init():
self.config = Config().config
self.config.update(self.get_config())

多个类继承基,a 类总是被 b 类改变。
debug 半天找不到问题。
又怀疑基类变量的共享,但变量也不在基类啊。
最后想起… Config()特码的是单例啊。

user3

运行了一下,打印出来不是 B{}吗?

user1

对的没错,b.proto_ 和 B.prototype 都是 A 的实例。

Object.getPrototypeOf(b) === B.prototype
> true

B.prototype instanceof A
> true

user2

恕我直言,这有意义吗

1
20 / 页
总数 111