gpt4 book ai didi

javascript - 无法理解javascript中的类到函数映射

转载 作者:行者123 更新时间:2023-11-30 09:39:15 25 4
gpt4 key购买 nike

class Parent {
print() {
console.log('hey i am parent');
}
}

class Child extends Parent {
constructor() {
super();
}
print() {
console.log('hey i am child');
}
}

x = new Parent();
console.log(Object.getPrototypeOf(x))
x.print();

虽然 x 的 [[prototype]] 是一个空对象,但它仍然可以访问 Parent 类中定义的 print() 函数。

我无法理解为什么 Object.getPrototypeOf(x) 是一个空对象。

最佳答案

它就在那里,只是不可枚举。试试这个:

Object.getOwnPropertyNames(Object.getPrototypeOf(x));

// ["constructor", "print"]

class Parent {
print() {
console.log('hey i am parent');
}
}

class Child extends Parent {
constructor() {
super();
}
print() {
console.log('hey i am child');
}
}

x = new Parent();
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(x)));
x.print();

关于javascript - 无法理解javascript中的类到函数映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42018167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com