简体   繁体   English

什么是JavaScript中对象的“内部插槽”?

[英]What is an “internal slot” of an object in JavaScript?

I've tried to understand ECMAScript 2015 specification in one point: Internal Slots of Objects . 我试图在一点上理解ECMAScript 2015规范: 对象的内部插槽 But this section appeared very unclear to me, especially this sentence: 但这部分对我来说显得很不清楚,特别是这句话:

Internal slots correspond to internal state that is associated with objects and used by various ECMAScript specification algorithms. 内部插槽对应于与对象关联并由各种ECMAScript规范算法使用的内部状态。

(Does it use correct grammar?) Can anybody explain this notion in English? (它使用正确的语法吗?)有人能用英语解释这个概念吗?


What I can understand so far: 到目前为止我能理解的是:

  • 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 内部插槽是或​​具有 ,最初是undefined

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. 它们可能或可能不对应于引擎使用的对象的属性,但它们不可用于用户代码,除非公共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. 引擎使用的实际实现可能与内部方法草拟的内容大不相同,但为了符合要求,它们必须产生与内部方法一致的行为或结果。

Examples 例子

[[StringData]] internal slot [[StringData]]内部插槽

The behavior of String , eg new String("whatever") , is described in terms that include a [[StringData]] internal slot that represents the value ( whatever in this case). String的行为,例如new String("whatever") ,用包含表示值的[[StringData]]内部槽的术语来描述(在这种情况下是whatever )。 The internal slot isn't directly accessible to user code, but String.prototype.toString() (eg (new String("whatever")).toString() ) is defined in terms of a thisStringValue() abstract operation, which is described in terms of returning the value of [[StringData]] . 用户代码不能直接访问内部槽,但String.prototype.toString() (例如(new String("whatever")).toString() )是根据thisStringValue()抽象操作定义的,在返回[[StringData]]的值方面进行了描述。 So in other words, String.prototype.toString() is public API that is essentially a getter that exposes [[StringData]] . 换句话说, String.prototype.toString()是公共API,它本质上是一个暴露[[StringData]]的getter。

[[OwnPropertyKeys]] internal method [[OwnPropertyKeys]]内部方法

The behavior of Object.keys() is described in terms that include calling the [[OwnPropertyKeys]] internal method. Object.keys()的行为用包括调用[[OwnPropertyKeys]]内部方法的术语来描述。 Note that different kinds of objects, such as ordinary objects (eg Object ) and exotic objects (eg String ) may have different definitions of [[OwnPropertyKeys]] . 请注意,不同类型的对象(例如普通对象(例如Object ))和外来对象(例如String )可能具有[[OwnPropertyKeys]]不同定义。 When [[OwnPropertyKeys]] is "called" in the spec, that refers to the definition for the applicable type. 在规范中“调用” [[OwnPropertyKeys]] ,它指的是适用类型的定义。 There are also some invariant characteristics that apply to its definition for any object type. 还有一些不变特征适用于任何对象类型的定义。

It's simply an artifice used to be able to describe precisely how the objects should behave. 它只是一种技巧,用于精确描述物体应如何表现。

They are not real members of the objects and even if in some implementation they are you are not allowed to access them with portable code. 它们不是对象的真实成员,即使在某些实现中,您也不允许使用可移植代码访问它们。

In other words it's a way to write the specification that allows describing behavior with imperative code that is formally more precise that just using a wordy "natural-language" description of what the behavior should be. 换句话说,它是一种编写规范的方法,该规范允许使用命令式代码描述行为,这种代码正式更精确,只是使用对行为应该是什么的罗嗦的“自然语言”描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM