[英]Is there a way to programmatically determine that a function is actually an object in Javascript?
In Javascript, functions are objects.在 Javascript 中,函数是对象。 Yet
typeof
of a function returns function
instead of object
for ECMAScript compatibility reasons .然而,出于ECMAScript 兼容性原因,function 的
typeof
返回function
而不是object
。
Is this function
type actually some kind of wrapper for object
that can be determined somehow?这个
function
类型实际上是某种可以以某种方式确定的object
的包装器吗? Or in Javascript, is a function
actually fundamentally different from object
s?或者在 Javascript 中,一个
function
实际上与object
有本质区别吗?
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?
但根据定义,它们是否实际上与 Javascript 中的对象完全分开,或者是否有办法以编程方式揭示 Function 的组成部分 Object?
an object is a member of the built-in type Object;
object 是内置类型 Object 的成员; and a function is a callable object
function 是可调用的 object
So所以
Or in Javascript, is a function actually fundamentally different from objects?
或者在 Javascript 中,function 实际上与对象根本不同吗?
Not really, except for the fact that a function is callable - more precisely, that it has a [[Call]]
internal method.不是真的,除了 function 是可调用的——更准确地说,它有一个
[[Call]]
内部方法。
Functions inherit from objects in a very similar way that objects inherit from objects.函数从对象继承的方式与对象从对象继承的方式非常相似。 For example, for a given function:
例如,对于给定的 function:
function foo(){}
foo.a = 'a';
There is:有:
[[Call]]
internal method, as well as an a
property, and a few other properties that functions have (like name
) [[Call]]
内部方法,还有一个a
属性,以及函数具有的一些其他属性(比如name
)Function.prototype
Function.prototype
Function.prototype
inherits from Object.prototype
Function.prototype
继承自Object.prototype
Object.prototype
inherits from nothing - that's the start of the prototype chain Object.prototype
继承——这是原型链的开始is there a way to programmatically reveal the Object that the Function is made of?
有没有办法以编程方式揭示 Function 的组成部分 Object?
That'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'
.事实并非如此 - function 本身是一个 object。如果 function object 具有属性,它将直接在 function 上可见。例如,上面引用
foo.a
将为您提供字符串'a'
。 The a
property isn't separate from the function - it's directly on the function object. a
属性与 function 没有分开 - 它直接位于 function object 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.