I have been wrapping my head around pseudoclassical inheritance in Javascript , and continuing on this topic I wanted to set an Static property/method , for a defined Constructor function.
I want to avoid the class
syntax for it.
function Food () {
isEdible = true;
}
console.log(Food.isEdible); // undefined
I know this example is wrong
I am wondering how it has been done on the rest of the Javascript Object model, with methods so important like for instance
Array.of(element1, element2, /* …, */ elementN)
Which can be accessed without the need of Instanciating an Array
2
Answers
Just assign to the function:
Array.of
is a static method. The docs:In class based inheritance, the way to write it would be:
The equivalent of above in prototype inheritance is:
Playground to view the conversion in ES5