3 ms·
Here's why a "class" definition is a "function" in JavaScript: Since JavaScript is prototypal and not class-based, the "new" operator simply creates a tabla ra
by eo3x0 14y ago
Here's why a "class" definition is a "function" in JavaScript:
Since JavaScript is prototypal and not class-based, the "new" operator simply creates a tabla rasa object. The argument to new is a function that runs on the new object with "this" in the scope set to the object.
Thus "new foo()" creates an blank object and runs foo on it. You can do this with any function!
If you don't try to force C++/Java semantics onto JS, it's much easier to understand.