What is an "internal slot" of an object in JavaScript?
Asked Answered
S

2

29

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
Stoker answered 12/10, 2015 at 7:15 Comment(4)
It's a specification device to define something that an object has that can't be accessed by native code (i.e. you can't access an internal slot, though things you do might affect its value). Note that the specification defines behaviour, so objects just act like they have internal slots. Whether they actually have them or not is irrelevant. E.g. String objects have an internal slot to hold an immutable string value. You can read it using valueOf but can't change it.Lelia
There's nothing wrong with the grammar, and it's written in perfectly good English; it just doesn't say very much of anything explicit.Cutup
@hobbs—and what it says is getting more turgid with each new edition… it's about comprehension, not grammar. ;-)Lelia
Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not"!Slipper
L
32

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.

Laney answered 12/10, 2015 at 12:54 Comment(4)
This is the most detailed and correct answer, so far. Please award 'Answer' to @JMM.Shetrit
@Laney Are you saying somewhere deep in JS engine, they have implemented some code that corresponds to [[OwnPropertyKeys]] method ? If yes, can i say , internal slots are internal variables in JS engine that will serve the purpose of what that internal slot is supposed to do as defined in ECMA ? Same for internal method ?Decagon
@Decagon A particular implementation might have an actual method corresponding to that slot, or it might just act as though it does. For instance, two different parts of the engine might have different implementations of the [[OwnPropertyKeys]] "method" that are optimised for different assumptions, but which are both guaranteed to produce the behaviour described in the spec.Enantiomorph
If it's completely an internal thing, then why is it exposed to us? We can't use them anyway.Bombproof
M
4

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.

Mccully answered 12/10, 2015 at 8:9 Comment(1)
I have read in book explaining like this - When a property is first added to an object, JavaScript uses an internal method called [[Put]] on the object.. If [[Put]] was just a language word , why does author says that [[Put]] method is called ? Is it hence, right to say , all these internal slots and methods are implemented in some way whether in JS engine or in object that they create.Decagon

© 2022 - 2024 — McMap. All rights reserved.