Forked from ericelliott/class-constructor-factory-examples.js
Created
February 2, 2018 15:07
-
-
Save SK-CSE/c33ff5cb61ea8bbf4330b71a9917ff8c to your computer and use it in GitHub Desktop.
Class, Constructor, Factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class | |
class ClassCar { | |
drive () { | |
console.log('Vroom!'); | |
} | |
} | |
const car1 = new ClassCar(); | |
console.log(car1.drive()); | |
// constructor | |
function ConstructorCar () {} | |
ConstructorCar.prototype.drive = function () { | |
console.log('Vroom!'); | |
}; | |
const car2 = new ConstructorCar(); | |
console.log(car2.drive()); | |
// factory | |
const proto = { | |
drive () { | |
console.log('Vroom!'); | |
} | |
}; | |
function factoryCar () { | |
return Object.create(proto); | |
} | |
const car3 = factoryCar(); | |
console.log(car3.drive()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment