An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
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.log("Hello")}
foo.prototype{constructor: }
Clicking on the arrow for the constructor, will return an object.
{constructor: }constructor: foo()arguments: nullcaller: nulllength: 0name: "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: nullcaller: nulllength: 0name: "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
Original Link: https://dev.to/dani8439/javascript-internal-property-1nl2
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To