简体   繁体   English

为什么在Function.prototype上没有原型属性?

[英]Why's there no a prototype property on Function.prototype?

This question is a bit pedantic. 这个问题有点古怪。 You've been warned beforehand :) 您已经被提前警告过:)

I noticed something and I thought of sharing it with you. 我注意到了一些事,并想与您分享。 Let's get straight to it. 让我们直接说吧。

Since all functions are supposed to have a prototype property that's epicenter of the prototypical inheritance model in js, and since Function.prototype is a function in itself, you'd expect to see a prototype property on the prototype object that all functions in js get their methods and properties from by virtue of inheritance but nothing there. 由于所有功能都应该具有原型属性 ,而该属性是js中原型继承模型的中心,并且因为Function.prototype本身就是一个函数,所以您希望在原型对象上看到一个prototype属性,以便js中的所有函数都能获得它们的方法和属性来自于继承,但那里一无所有。

Is there a logical explanation behind this decision to omit the prototype on that particular function or it was dropped just for stylistic reasons since Function.prototype.prototype might look ugly for some, or maybe the prototype prop on the Function constructor is like for the lack of a better term the god of all prototypes in js and thus can't have a prototype property itself? 在此决定之后,是否有逻辑上的解释来省略该特定函数上的prototype ,还是出于风格原因而将其删除,因为Function.prototype.prototype可能对某些人来说看起来很丑陋,或者Function构造Function上的prototype道具就像缺少它一样更好的术语是js中所有原型的上帝 ,因此本身不能拥有prototype属性?

Looking forward to your answers. 期待您的回答。

Happy coding :) 快乐的编码:)

即使您的浏览器报告“功能”, 原型也是object或null。

"all functions are supposed to have a prototype property" “所有功能都应该具有原型属性”

That has never been true, and is even less true in ES6: 这从未如此,而在ES6中则更是如此:

Array.prototype.slice.hasOwnProperty('prototype')  // false
let f = () => {}
f.hasOwnProperty('prototype')  // false
let o = { m() {} }
o.m.hasOwnProperty('prototype')  // false
class C { m() {} }
(new C).m.hasOwnProperty('prototype')  // false

JavaScript distinguishes between functions, and functions that are constructors. JavaScript区分函数和作为构造函数的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM