有没有办法以编程方式确定函数实际上是 Javascript 中的对象?

发布于 2025-01-17 20:50:09 字数 461 浏览 3 评论 0原文

在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 objects?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2025-01-24 20:50:09

作为规格

一个对象是内置类型对象的成员;函数是一个可呼叫对象

所以

或在JavaScript中,一个函数实际上与对象根本不同吗?

除了一个函数是可召唤的事实 - 更确切地说,它具有 [[call]] 内部方法。

函数以非常相似的方式从对象继承,该对象从对象继承。例如,对于给定函数:

function foo(){}
foo.a = 'a';

有:

  • foo函数,它是一个对象,并且具有[[call]]内部方法,以及a属性,以及功能具有的其他一些属性(例如name
  • foo函数从function.prototype
  • function.prototype.prototype继承。 > object.protype
  • object.prototype从无到有 - 这是原型链的开始

是否有一种方法可以编程地揭示该函数由?

的对象

并非如此 - 函数本身就是对象。如果函数对象具有属性,则将直接在函数上看到。例如,上面,引用foo.a将为您提供字符串'a'a属性与函数并非分开 - 它直接在函数对象上。

As the spec says:

an object is a member of the built-in type Object; and a function is a callable object

So

Or in Javascript, is a function actually fundamentally different from objects?

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:

function foo(){}
foo.a = 'a';

There is:

  • The foo function, which is an object, and has a [[Call]] internal method, as well as an a property, and a few other properties that functions have (like name)
  • The foo function inherits from Function.prototype
  • Function.prototype inherits from Object.prototype
  • Object.prototype inherits from nothing - that's the start of the prototype chain

is there a way to programmatically reveal the Object that the Function is made of?

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'. The a property isn't separate from the function - it's directly on the function object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文