我刚开始阅读有关Javascript,我似乎无法找到这个问题的asnwer.Suppose我有一个函数称为test
和一个匿名函数分配给一个变量说temp
。我注意到,当我使用例2中的变量创建新对象实例时,没有分配__proto__
?这是为什么使用新的变量分配了一个函数没有分配__proto__
function test() {
this.name = "testerName";
}
var temp = function() {
this.name = "testeragain";
}
// Example 1:
var d = new test();
console.log(d.__proto__) //prints test{}
console.log(d.__proto__ == test.prototype) //Returns false ? shouldnt this
// Example 2:
var d = new temp();
console.log(d.__proto__) //prints {} ?Why is this?
我正在使用命令node test.js
因为您的调试器选择不命名它。注意,'d'都从它们各自的原型继承而来 - 你将得到相同的结果'console.log(test.prototype); console.log(temp.prototype);' – Bergi
你应该使用'Object.getPrototypeOf(d)'。 '__proto__' getter已被弃用。 – Bergi
@Bergi我尝试使用'Object.getPrototypeOf(d)',我得到相同的结果。那可能是因为我使用节点来运行js文件? –