JavaScript Internal Property`[[]]`

Photo by Dayne Topkin on Unsplash

Maybe one day you were playing in the dev browser, and one day come across something that seemed a bit different.

You’ve printed things out to the console, and something odd appears.

function foo() {
console…


This content originally appeared on DEV Community and was authored by Dani Schuhman

Photo by Dayne Topkin on Unsplash

Maybe one day you were playing in the dev browser, and one day come across something that seemed a bit different.

You've printed things out to the console, and something odd appears.

function foo() { 
    console.log("Hello")
}
foo.prototype
{constructor: ƒ}

Clicking on the arrow for the constructor, will return an object.

{constructor: ƒ}
constructor: ƒ foo()
arguments: null
caller: null
length: 0
name: "foo"
prototype: {constructor: ƒ}
[[FunctionLocation]]: VM572:1
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[2]
[[Prototype]]: Object

What on earth are those double brackets [[]]?

It's the internal property. In JavaScript, objects have an internal property known as Prototype. You can also see that there is a Scopes inside of these double brackets as well once clicking inside an object.

Whenever there are [[]] that appear, it's an internal property that can't be accessed by our code. Both Scopes and Prototype are internal properties of the foo object.

What's pretty cool, and also very helpful when clicking on the Scopes internal property, is that when working with some concepts, say, a closure, clicking on the scopes property will show the closure itself.

let f;

const g = function() {
    const a = 23;
    f = function() {
        console.log(a * 2);
    };
};

g();
f();

console.dir(f)

// Returns
ƒ f()
arguments: null
caller: null
length: 0
name: "f"
prototype: {constructor: ƒ}
[[FunctionLocation]]: VM495:3
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[3]

Clicking on the Scopes internal property, we can see where the closure lives.

[[Scopes]]: Scopes[3]
0: Closure (g) {a: 23}
1: Script {f: ƒ, g: ƒ}
2: Global {0: Window, window: Window, self: Window, docum...

It's pretty cool, isn't it?

Further Reading

StackOverFlow

JavaScript Info - Private Protected Properties


This content originally appeared on DEV Community and was authored by Dani Schuhman


Print Share Comment Cite Upload Translate Updates
APA

Dani Schuhman | Sciencx (2021-10-29T17:20:57+00:00) JavaScript Internal Property`[[]]`. Retrieved from https://www.scien.cx/2021/10/29/javascript-internal-property/

MLA
" » JavaScript Internal Property`[[]]`." Dani Schuhman | Sciencx - Friday October 29, 2021, https://www.scien.cx/2021/10/29/javascript-internal-property/
HARVARD
Dani Schuhman | Sciencx Friday October 29, 2021 » JavaScript Internal Property`[[]]`., viewed ,<https://www.scien.cx/2021/10/29/javascript-internal-property/>
VANCOUVER
Dani Schuhman | Sciencx - » JavaScript Internal Property`[[]]`. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/29/javascript-internal-property/
CHICAGO
" » JavaScript Internal Property`[[]]`." Dani Schuhman | Sciencx - Accessed . https://www.scien.cx/2021/10/29/javascript-internal-property/
IEEE
" » JavaScript Internal Property`[[]]`." Dani Schuhman | Sciencx [Online]. Available: https://www.scien.cx/2021/10/29/javascript-internal-property/. [Accessed: ]
rf:citation
» JavaScript Internal Property`[[]]` | Dani Schuhman | Sciencx | https://www.scien.cx/2021/10/29/javascript-internal-property/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.