Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an "internal slot" of an object in JavaScript?

Tags:

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:

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?


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
like image 552
BairDev Avatar asked Oct 12 '15 07:10

BairDev


1 Answers

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.

Examples

[[StringData]] internal slot

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

[[OwnPropertyKeys]] internal method

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.

like image 125
JMM Avatar answered Sep 28 '22 10:09

JMM