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
JavaScript Info - Private Protected Properties
This content originally appeared on DEV Community and was authored by Dani Schuhman
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.