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

查看:41
本文介绍了什么是“内部插槽"?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?

目前我能理解的:

  • 内部插槽不是属性
  • 在创建对象期间使用内部插槽,但不会添加到对象本身
  • 内部槽是或具有,最初是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]] 内部槽的术语来描述,该槽表示值(whatever in这个案例).用户代码不能直接访问内部插槽,但是 String.prototype.toString()(例如 (new String("whatever")).toString())定义在一个thisStringValue()抽象操作中,它是根据返回[[StringData]]的值来描述的.所以换句话说,String.prototype.toString() 是公共 API,它本质上是一个暴露 [[StringData]] 的 getter.

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]].

Object.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天全站免登陆