JS构造函数的原型属性与其原型之间的区别

Difference between the prototype property of a JS constructor and it's prototype

本文关键字:原型 之间 区别 属性 构造函数 JS      更新时间:2023-09-26

构造函数:

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是一个

函数,因此它继承了Function.prototype的所有属性。 Function也是一个(继承自)object,因此它具有来自Object.prototype.的所有属性 但是,Object.getPrototypeOf 是 Object 上的"静态"方法,因此它不是继承的。

Object.getPrototypeOf(Team)指向与Function.prototype相同的对象。 Team.getPrototypeOf未定义。