我无法真正理解 console.log 最后的输出。
打印:
constructor - ƒ Array() { [native code] }
speak - undefined
undefined
1) 为什么speak - 未定义? 2)为什么有第三个输出行 - 只是 - 未定义?它从哪里来?
class Bar {
speak() {
let text = 'test';
alert(text);
return text;
}
}
var test = new Bar;
z = Object.getOwnPropertyNames(Object.getPrototypeOf(test));
console.log(typeof z, " - ", z);
console.log(z.forEach(function(e) { console.log(e, " - ", z[e]); }));
请帮忙。谢谢!
最佳答案
您正在检查 z[e]
而不是 test[e]
。 z
是属性名称数组,数组没有 speak
属性,与 Bar
对象不同,后者有 speak
属性。
这解释了 speak - undefined
行。最终的独立undefined
是console.log
本身的返回值:Chrome/Firefox console.log always appends a line saying undefined
关于javascript - 如何理解 JavaScript 中类实例提供的 forEach 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46452339/