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
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.
[[StringData]] internal slot
The behavior of
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
(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
[[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]] 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.
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.