05. Enforce new
function Vehicle(type, wheelsCount) {
this.type = type;
this.wheelsCount = wheelsCount;
return this;
}
// Function invocation
const car = Vehicle("Car", 4);
car.type; // => 'Car'
car.wheelsCount; // => 4
car === window; // => truefunction Vehicle(type, wheelsCount) {
if (!(this instanceof Vehicle)) {
throw Error("Error: Incorrect invocation");
}
this.type = type;
this.wheelsCount = wheelsCount;
return this;
}
// Constructor invocation
const car = new Vehicle("Car", 4);
car.type; // => 'Car'
car.wheelsCount; // => 4
car instanceof Vehicle; // => true
// Function invocation. Generates an error.
const brokenCar = Vehicle("Broken Car", 3);new.target property
new.target propertyLast updated