有没有办法以编程方式确定函数实际上是 Javascript 中的对象?
在JavaScript中,函数是对象。但是typeof
的函数返回function
而不是object
for ecmascript兼容性原因。
此函数
实际上可以以某种方式确定对象
的某种包装器吗?还是在JavaScript中,函数
实际上与对象
s根本不同?
我确实知道函数是有效的和实际上对象的。但是,根据定义,它们是实际上与JavaScript中的对象完全分开的,还是有没有办法可以编程地揭示该函数所制成的对象?
In Javascript, functions are objects. Yet typeof
of a function returns function
instead of object
for ECMAScript compatibility reasons.
Is this function
type actually some kind of wrapper for object
that can be determined somehow? Or in Javascript, is a function
actually fundamentally different from object
s?
I do understand that functions are effectively and practically objects. But are they by definition actually completely separate from Objects in Javascript, or is there a way to programmatically reveal the Object that the Function is made of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为规格说:
所以
除了一个函数是可召唤的事实 - 更确切地说,它具有
[[call]]
内部方法。函数以非常相似的方式从对象继承,该对象从对象继承。例如,对于给定函数:
有:
[[call]]
内部方法,以及a
属性,以及功能具有的其他一些属性(例如name
)function.prototype
function.prototype.prototype
继承。 > object.protypeobject.prototype
从无到有 - 这是原型链的开始并非如此 - 函数本身就是对象。如果函数对象具有属性,则将直接在函数上看到。例如,上面,引用
foo.a
将为您提供字符串'a'
。a
属性与函数并非分开 - 它直接在函数对象上。As the spec says:
So
Not really, except for the fact that a function is callable - more precisely, that it has a
[[Call]]
internal method.Functions inherit from objects in a very similar way that objects inherit from objects. For example, for a given function:
There is:
[[Call]]
internal method, as well as ana
property, and a few other properties that functions have (likename
)Function.prototype
Function.prototype
inherits fromObject.prototype
Object.prototype
inherits from nothing - that's the start of the prototype chainThat's not really how it is - the function itself is an object. If the function object has properties, it'll be visible directly on the function. For example, above, referencing
foo.a
will give you the string'a'
. Thea
property isn't separate from the function - it's directly on the function object.