简体   繁体   English

有没有办法以编程方式确定 function 实际上是 Javascript 中的 object?

[英]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?

As the spec says :正如规范所说

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:有:

  • 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 ) foo function,它是一个 object,并且有一个[[Call]]内部方法,还有一个a属性,以及函数具有的一些其他属性(比如name
  • The foo function inherits from Function.prototype foo function 继承自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.

相关问题 确定var是否为javascript中的elementFinder对象的方法是什么? - What is a way to determine if a var is an elementFinder object in javascript? 这是一种可接受的方法来确定对象是否是JavaScript中的数组? - Is this an acceptable way to determine if an object is an array in JavaScript? 有没有一种方法可以确定我的对象方法是否是函数 - Is there a way to determine rather my object method is a function or not 异步函数,用于确定JavaScript对象中属性的值 - Async Function to determine the value of a property in a JavaScript object 确定JavaScript中对象的类 - Determine class of an object in javascript 有没有办法从网站的源代码以编程方式运行 javascript function - Is there a way to programmatically run a javascript function from the source of a website 有没有办法以编程方式确定输入日期字段是否不完整? - Is there a way to programmatically determine if an input date field is incomplete? 确定用户是否实际使用Safari的最佳方法是什么? - What is best way to determine if the user is actually using Safari? 有没有办法确定何时实际渲染字体系列 - Is there a way to determine when font family being actually rendered JavaScript:如何以编程方式确定网络“折叠”? - JavaScript: How to determine the web “fold” programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM