...

/

Solution Review: Instance Type

Solution Review: Instance Type

Let’s discuss the solution to the challenge given in the previous lesson.

Solution

Let’s break down the solution to the challenge given in the previous lesson.

Press + to interact
'use strict';
class Base {
copy() {
const constructor =
Reflect.getPrototypeOf(this).constructor[Symbol.species] ||
Reflect.getPrototypeOf(this).constructor;
return new constructor();
}
}
class Derived1 extends Base {
static get [Symbol.species]() {
return Base;
}
}
class Derived2 extends Base {
static get [Symbol.species]() {
return Derived2;
}
}
const derived1 = new Derived1();
const derived2 = new Derived2();
console.log(derived1.copy()); //Base {}
console.log(derived2.copy()); //Derived2 {}

Explanation

As you know, you can change the behavior of an instance type by implementing ...

Access this course and 1400+ top-rated courses and projects.