Per Classes in MDN
Classes are in fact "special functions"
How would something like
class Foo {
constructor(x) {
this.x = x;
}
foo() {}
}
be translated to just function
(ignoring the "hoisting" feature)
Per Classes in MDN
Classes are in fact "special functions"
How would something like
class Foo {
constructor(x) {
this.x = x;
}
foo() {}
}
be translated to just function
(ignoring the "hoisting" feature)
// declare the "class"
function Foo (x) {
this.x = x;
}
// declare the instance method
Foo.prototype.foo = function () {
return 'i am foo';
}
// create an instance
const f = new Foo(42);
// invoke its methods
console.log(f.foo()); // 'i am foo'
// access its properties
console.log(f.x); // 42
Non hoisted, (as much as constructor functions based on class syntax are not hoisted,) due to an immediately invoked function expression (IIFE) which serves as module pattern ...
const Foo = (function () { // class Foo {
//
function Foo(x) { // constructor(x) {
this.x = x; // this.x = x;
} // }
function foo() { // foo() {
} //
Foo.prototype.foo = foo; // }
//
return Foo; //
//
}()); // }
Well really everything is objects - a class is simply a blueprint for an object that has some methods and some values. It's all objects with features that can either store values or transform values.
A class is something - a function does something. Calling it a special function is if anything unnecessary at best and confusing at worst.
Basically - don't worry about it.