Function对象与Normal对象有何不同? [英] How are Function objects different from Normal objects?

查看:148
本文介绍了Function对象与Normal对象有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将函数存储在变量中?

How are functions stored in a variable?

根据 MDN


在JavaScript中,函数是第一类对象,因为它们可以像任何其他对象一样拥有属性和方法。它们与其他对象的区别在于可以调用函数。简而言之,它们是功能对象。

假设这个场景,

var fn = function () {};
fn.attr = 3;

console.log(fn); //prints function() {}

console.log(Object.keys(fn)); //prints ["attr"]

如果一个函数是一个对象,它不应该有键和函数存储在属性中的值类型,解释器不显示该属性?类似于C ++样式的运算符重载或Javascript本身中的数组/对象表示。我的意思是说函数(或数组)只是以不同方式处理的对象吗?这可能意味着匿名函数存储在具有隐藏属性的对象中。

If a function is an object shouldn't it have keys and value kind where the function is stored in a property and the interpreters don't show that property? Something like C++ style operator overloading or array/object representation in Javascript itself. I mean to say are functions (or arrays) are just objects treated in a different manner? Which might imply that anonymous functions are stored in an object with a hidden property.

总之,什么是基础工作函数(或数组)?他们是专门治疗的吗?或者它们只是一些隐藏属性的语法糖,当使用()时调用它?

In summary, what is the underlying working of functions(or arrays)? Are they treated specially? Or are they just syntactic sugar for some hidden property which is called when () is used?

推荐答案

是的,功能很特别。

证明

const f = Object.create(Function.prototype);
f(); // TypeError: f is not a function

博览会

它们是可调用对象,只能通过规定的语法创建(函数表达式,语句,声明,胖箭头,对象文字方法定义简写,函数构造函数,方法)。

They are "callable objects" that can only be created via prescribed syntax (function expressions, statements, declarations, fat-arrows, object literal method definition shorthand, Function constructor, class methods).

这意味着他们有一个特殊的 [[Call]] 使用()语法调用它们时调用的方法(对您不可见)。

That means they have a special [[Call]] method (not visible to you) that is called when you invoke them with the () syntax.

[[Call]] coordinates:

[[Call]] coordinates:


  • 创建执行上下文(call-stack frame)

  • 将新执行上下文添加到堆栈顶部

  • 执行函数逻辑

  • 从堆栈中删除执行上下文

  • 提示下一个要运行的上下文(紧接着一个上下文)在堆栈上)

  • 向下一个执行上下文提供任何返回值

  • the creation of the execution context (call-stack frame)
  • addition of the new execution context to the top of the stack
  • execution of the function logic
  • removal of the execution context from the stack
  • cueing up the next context to run (the one immediately lower on the stack)
  • supplying any return value to the next execution context

创建执行上下文依次完成 LexicalEnvironment (用于作用域)的配置,接收器的配置( this )函数和其他元逻辑。

Creation of the execution context in turn completes configuration of the LexicalEnvironment (used for scoping), configuration of the receiver (this) for the function and other meta-logic.

函数也与大多数普通对象不同,因为它们有 Function.prototype on他们的 [[Prototype]] 链(尽管你可以通过继承 Function.prototype 创建自己的'无用'对象 - 见ab ove)。

Functions also differ from most normal objects in that they have Function.prototype on their [[Prototype]] chain (although you can create your own 'useless' object by inheriting from Function.prototype - see above).

有关差异的完整列表,请参阅 MDN 规范等。

For a full list of differences, please see MDN, the spec etc.

这篇关于Function对象与Normal对象有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆