Function.prototype // function Empty() {}
它有什么意义?例如,如果我们将采用Number
对象,我们可以看到他的原型(Number.__proto__
)是Function.prototype
,其中包含apply
和call
等方法。如果Number的原型是一个空函数而不是像所有其他原型那样的常规原型对象,我如何使用Number.apply(..)
? (数字原型,字符串原型,任何其他自定义原型都是对象。甚至Object.prototype也是一个对象。)
之后,Object.__proto__ == Function.prototype
有什么意义?对象应该是最高的对象,当Function.prototype
继承自Function
时,它从Object.prototype
继承的方式!
Object instanceof Function // true
Function instanceof Object // of course true
Function instanceof Function // true
答案 0 :(得分:1)
Object.__proto__ == Function
表示Object
本身就是一个函数,因为它是构造函数。这并不意味着继承自Object
的对象将继承Function
。对象继承构造函数的.prototype
,而不是其.__proto__
换句话说
function Car (){}
inst = new Car ();
// inst inherits from Car.prototype
// inst.__proto__ == Car.prototype;
// Car inherits from Function.prototype because it is a function
// Car.__proto__ == Function.prototype;
但这并不意味着inst
继承自Function.prototype
,您无法在其上调用apply
和call
。
// This means that Everything that inherits from function will
console.log(`Function.prototype`) === function Empty() {}
另一个转折
// This means that the constructor function (Object)
// inherits from `Function.prototype` That is, you can use call and apply,
// And at a lower language level, you can use () and new on it.
Object instanceof Function // true
// It doesn't mean that instances created from Object inherit
// from Function.prototype (can't use call/apply)
(new Object()) instanceOf Function ? // false
(new Object()).apply === undefined ? // true
// This means that functions themselves are objects, everything is an object
// They have properties like hasOwnProperty and isPrototypeOf
// Not that everything that inherits from Object.prototype will also inherit
// From Function.prototype
Function instanceof Object // of course true