【发布时间】:2015-05-15 06:15:18
【问题描述】:
构造函数:
function Team (type) {
this.type = type;
}
//this will output this empty object inherited from Object.property
console.log(Team.prototype);
-> Team {}
//this one outputs nothing in my console
console.log(Object.getPrototypeOf(Team));
//is it inheriting from this one, the one for all functions?
-> Function.prototype //??
.prototype 属性和Object.getPrototypeOf 有什么区别?
除了存储属性之外,Function.prototype(所有函数和构造函数都继承自)原型还有什么作用?
【问题讨论】:
-
问题是“为什么
Team有一个属性getPrototypeOf”? -
问题是
Team.prototype和Team.getPrototypeOf有什么区别。 -
我想您希望
console.log(Object.getPrototypeOf(Team))查看所有函数继承自的对象。 -
@StevensHaen
Object.getPrototypeOf()返回实例主动继承的对象。函数的prototype属性是该函数的new实例将继承的内容。所以,var t = new Team(); console.log(Object.getPrototypeOf(t) === Team.prototype);。构造函数本身就是new Functions,继承自Function.prototype。 -
@StevensHaen:嗯,那是因为
Function.prototype = function(){}(出于可疑的原因,但it's what the spec says)。该函数对象确实具有call、apply、bind等预期属性。
标签: javascript