ES 5 - 为什么 Function.prototype 对象没有 属性 原型?
ES 5 - Why Function.prototype object does not have property prototype?
我了解到任何 function
类型的对象都有 属性 prototype
.
例如:
Object
有 属性 prototype
Function
有 属性 prototype
Person
有 属性 prototype
但是,
> typeof Function.prototype
"function"
我有两个问题,
1) 为什么 function
类型的对象 Function.prototype
在下面的可视化中没有自己的 属性 prototype
?
2) 任何对象通常继承自object
类型对象,但在下面的可视化中,Function
对象和Person
对象继承自function
类型对象Function.prototype
?
那么,对于以上两个问题,Function.protoype
类型 object
是否更安全?
如果你比较JavaScript中常见类型的原型,你会发现它们的原型是实际类型的一个实例:
Function.prototype = function () {}
Array.prototype = []
Object.prototype = {}
我不确定为什么会这样,但我想这是一个 ECMA 标准,或者是浏览器自己实现的东西,所以一切都有意义。换句话说,当 Function 的原型是一个类似于 Object 原型的对象时,你怎么知道 Function 是一个 Function。
英语不是我的母语,但我希望我表达清楚了。
但是回答你的问题:我猜函数的原型是一个例外,没有自己的原型,因为它不需要。
Why function type object Function.prototype
does not have its own property prototype
, in the below visualisation?
为什么会这样,您希望它指向什么?它不是构造函数。 (实际上,对于 ES6, 没有 .prototype
方法)。
很奇怪 Function.prototype
是一个可调用对象(函数),实际上没有任何好处。它也可能是一个普通的物体。 "The spec said so" 是我们所有的(另见 these questions)。
Any object usually inherits from an object type object, but the Function
object and Person
object inherit from function type object Function.prototype
?
嗯,是的,他们为什么不呢?正如您自己所说,函数只是对象,每个对象都继承自另一个对象(或 null
)。有些对象是可调用的(并且 typeof
为它们产生 "function"
)对继承没有任何影响。
我了解到任何 function
类型的对象都有 属性 prototype
.
例如:
Object
有 属性prototype
Function
有 属性prototype
Person
有 属性prototype
但是,
> typeof Function.prototype
"function"
我有两个问题,
1) 为什么 function
类型的对象 Function.prototype
在下面的可视化中没有自己的 属性 prototype
?
2) 任何对象通常继承自object
类型对象,但在下面的可视化中,Function
对象和Person
对象继承自function
类型对象Function.prototype
?
那么,对于以上两个问题,Function.protoype
类型 object
是否更安全?
如果你比较JavaScript中常见类型的原型,你会发现它们的原型是实际类型的一个实例:
Function.prototype = function () {}
Array.prototype = []
Object.prototype = {}
我不确定为什么会这样,但我想这是一个 ECMA 标准,或者是浏览器自己实现的东西,所以一切都有意义。换句话说,当 Function 的原型是一个类似于 Object 原型的对象时,你怎么知道 Function 是一个 Function。 英语不是我的母语,但我希望我表达清楚了。
但是回答你的问题:我猜函数的原型是一个例外,没有自己的原型,因为它不需要。
Why function type object
Function.prototype
does not have its own propertyprototype
, in the below visualisation?
为什么会这样,您希望它指向什么?它不是构造函数。 (实际上,对于 ES6,.prototype
方法)。
很奇怪 Function.prototype
是一个可调用对象(函数),实际上没有任何好处。它也可能是一个普通的物体。 "The spec said so" 是我们所有的(另见 these
Any object usually inherits from an object type object, but the
Function
object andPerson
object inherit from function type objectFunction.prototype
?
嗯,是的,他们为什么不呢?正如您自己所说,函数只是对象,每个对象都继承自另一个对象(或 null
)。有些对象是可调用的(并且 typeof
为它们产生 "function"
)对继承没有任何影响。