什么是“内部槽” JavaScript中的一个对象? [英] What is an "internal slot" of an object in JavaScript?

查看:118
本文介绍了什么是“内部槽” JavaScript中的一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了解一下ECMAScript 2015规范:对象的内部插槽。但是这一节对我来说非常不清楚,特别是这句话:

I've tried to understand ECMAScript 2015 specification in one point: Internal Slots of Objects. But this section appeared very unclear to me, especially this sentence:


内部插槽对应于与对象相关联并被各种使用的内部状态ECMAScript规范算法。

Internal slots correspond to internal state that is associated with objects and used by various ECMAScript specification algorithms.

(它是否使用正确的语法?)任何人都可以用英文解释这个概念?

(Does it use correct grammar?) Can anybody explain this notion in English?

目前为止我可以理解:


  • 内部插槽不是属性

  • 在创建对象期间使用内部插槽,但未添加到对象本身

  • 内部插槽是或​​具有>值,最初未定义

  • internal slots are not properties
  • internal slots are used during the creation of an object, but not added to the object itself
  • internal slots are or have values, initially undefined

推荐答案

摘要



内部插槽/方法是规范用于定义所需行为的伪属性/方法。 (抽象操作是规范的相关机制)。插槽表示状态(值),方法描述算法(行为)。它们可能或可能不对应于引擎使用的对象的属性,但除了公开API的某些部分外,它们不可用于用户代码。引擎使用的实际实现可能与内部方法草案的内容截然不同,但要符合规定,则必须产生与内部方法一致的行为或结果。

Summary

Internal slots / methods are pseudo-properties / -methods that the specification uses to define required behavior. ("Abstract operations" are a related mechanism of the spec.) Slots represent state (values), and methods describe algorithms (behavior). They may or may not correspond to properties of objects used by the engine, but they're not available to user code, except as exposed by some part of the public API. The actual implementation an engine uses may be very different from what the internal methods sketch out, but to be compliant they have to produce behavior or results that are consistent with the internal methods.

String 的行为,例如 new String(whatever),用包含 [[StringData]] 在这种情况下,值(无论)。内部插槽不能直接访问用户代码,但 String.prototype.toString()(例如(new String(whatever)) .toString())根据 thisStringValue()抽象操作定义,该操作以返回值 [[StringData是]] 。所以换句话说, String.prototype.toString()是公开的API,本质上是一个暴露了 [[StringData]]

The behavior of String, e.g. new String("whatever"), is described in terms that include a [[StringData]] internal slot that represents the value (whatever in this case). The internal slot isn't directly accessible to user code, but String.prototype.toString() (e.g. (new String("whatever")).toString()) is defined in terms of a thisStringValue() abstract operation, which is described in terms of returning the value of [[StringData]]. So in other words, String.prototype.toString() is public API that is essentially a getter that exposes [[StringData]].

对象的行为.keys() 的描述包括调用 [[OwnPropertyKeys]] 内部方法。请注意,不同类型的对象,如普通对象(例如 Object )和异国情调的对象(例如 String )可能会有不同的定义 [[OwnPropertyKeys]] 。当 [[OwnPropertyKeys]] 在规范中被调用时,它指的是适用类型的定义。还有一些不变量适用于任何对象类型的定义的特征。

The behavior of Object.keys() is described in terms that include calling the [[OwnPropertyKeys]] internal method. Note that different kinds of objects, such as ordinary objects (e.g. Object) and exotic objects (e.g. String) may have different definitions of [[OwnPropertyKeys]]. When [[OwnPropertyKeys]] is "called" in the spec, that refers to the definition for the applicable type. There are also some invariant characteristics that apply to its definition for any object type.

这篇关于什么是“内部槽” JavaScript中的一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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