0

Why the following code returns true?

class MyClass {
  constructor(a, b) { }
}

console.log(MyClass.constructor === Function);

According to this question: are es6 classes just syntactic sugar for the prototypal pattern in javascript? a class is a function which is filled with the constructor's body at runtime, which might explain the behavior above, but I still don't understand it very well. Can someone explain?

Guerric P
  • 30,447
  • 6
  • 48
  • 86
  • `MyClass` is a function itself and its type is...`MyClass`. But the thing that generate that function is not a different *custom* function. – VLAZ Sep 12 '19 at 12:02
  • all class constructors extend `Function`, and all class instances extend `Object`. This means that classes are actually themselves instances of `Function` – Patrick Roberts Sep 12 '19 at 12:02
  • I think you might be misinterpreting stuff here - `MyClass.constructor` is NOT the `constructor` in your `class MyClass` - `MyClass` itself *is* the constructor. If you call `new MyClass("alpha", "beta")` you are going to execute the code inside `constructor`. – VLAZ Sep 12 '19 at 12:03
  • @VLAZ `s/call/construct/` – Patrick Roberts Sep 12 '19 at 12:07
  • @PatrickRoberts just trying to avoid [semantic satiation](https://en.wikipedia.org/wiki/Semantic_satiation) but you're correct. – VLAZ Sep 12 '19 at 12:08
  • Possible duplicate of [are es6 classes just syntactic sugar for the prototypal pattern in javascript?](https://stackoverflow.com/questions/36419713/are-es6-classes-just-syntactic-sugar-for-the-prototypal-pattern-in-javascript) – Adersh M Sep 12 '19 at 12:15

2 Answers2

0

The constructor property of any kind of JavaScript object is actually what has been used to construct it:

A class is actually a function, so it is constructed from Function. That's why MyClass.constructor === Function

The constructor of a class is actually the class itself. It is also located at MyClass.prototype.constructor to make it available from the constructor property of an instance. Indeed, MyClass === MyClass.prototype.constructor

Guerric P
  • 30,447
  • 6
  • 48
  • 86
0

Constructor is just a function to create custom object type and create object’s instances using new keyword.

 /**
 * `Constructor function` that defines custom data object, commonly named using capital letter.
 * The properties will be available for every object created from this function,
 * because `this` points to the instance of the object.
 */

function Human(name, lastName) {
    this.name = name;
    this.lastName = name;
}

/**
 * Objects are created from `constructor function` using `new` keyword.
 * Parameters that are passed to the function will be the values of object's properties.
 */
const american = new Human('Mike', 'Brown');

Inside classes the constructor method is actually constructor function - what creates instances of an object.

class AnotherHuman{
    constructor(name, age){
        this.name = name;
        this.age = age;
    }

const student = AnotherHuman('Jack', 22);