ECMAScript 2015 specification came up with the idea of internal slots and internal methods that specify the internals properties and methods of the object not exposed to runtime.

The 6.1.7.2 section of the ECMAScript 2015 specification talks about some weird internal properties and internal methods objects (_descendants of __Object_) can have. These properties or methods are implemented by the JavaScript engine but they are abstracted away from the runtime, hence you won’t be able to access them on the objects like normal properties.

These are represented by the [[<name>]] notation in the ECMAScript specification where name is the name of the internal property or internal method. The internal property is called an internal slot and it contains a value associated with that object to represent some state of the object.

Let’s take a quick example. The [[GetPrototypeOf]] internal method is implemented by all the objects and its job is to return the prototype of the object. When you execute Reflect.getPrototypeOf(_obj_) method with obj being the object whose prototype needs to be inspected, JavaScript engine calls the [[GetPrototypeOf]] internal method of the obj which returns the value of [[Prototype]] internal slot of the object that contains the prototype.

💡 The _obj.__proto___ also points to the prototype of the _obj_ and accessing it would be like accessing the _[[Prototype]]_ internal slot of the _obj_.

When an internal method is invoked on an object such as obj in the above example, it is called the “target” of the invocation. If the target doesn’t support an internal method, for example calling the Reflect.getPrototypeOf on null, a TypeError exception is thrown.

Objects can have multiple internal slots and internal methods. ECMAScript specification does not describe how these internal methods are implemented but it describes the signature of the method call. The following are ES2015 internal methods implemented by all objects (hence essential). These internal methods may accept some arguments of specific ECMAScript language types and may return a value.

Image for post

#javascript #ecmascript-6 #ecmascript #es6 #ecmascript2015

What are “Internal Slots” and “Internal Methods” in JavaScript?
7.75 GEEK