A bit of confusion on how static
works in javascript.
function StaticExample() {}
StaticExample.staticProp = "Hello"
console.log(StaticExample.staticProp) // "Hello"
Here I have created a function
and then created a static variable staticProp
(not if we can call it static though). This adds the field staticProp
to the constructor
field on StaticExample
's prototype
Question 1: Is there a specific reason to this behaviour(field getting added to the constructor property)?
Now the constructor
is rewritten as below.
StaticExample.prototype.constructor = {}
console.log(StaticExample.staticProp) // "Hello"
Even after that, when I tried to access StaticExample.staticProp
, it provides the correct value of Hello
Question 2: From where the staticProp
fetched, even when the constructor
where it was initially added is overwritten.