1function ConstructorFunction() {
2 this.someProp1 = "1";
3 this.someProp2 = "2";
4}
5ConstructorFunction.prototype.someMethod = function() { /* whatever */ };
6
7function factoryFunction() {
8 var obj = {
9 someProp1 : "1",
10 someProp2 : "2",
11 someMethod: function() { /* whatever */ }
12 };
13 // other code to manipulate obj in some way here
14 return obj;
15}
16
1// constructor
2function ConstructorCar () {}
3ConstructorCar.prototype.drive = function () {
4 console.log('Vroom!');
5};
6
7const car2 = new ConstructorCar();
8console.log(car2.drive());
9
10// factory
11const proto = {
12 drive () {
13 console.log('Vroom!');
14 }
15};
16
17const factoryCar = () => Object.create(proto);
18const car3 = factoryCar();
19console.log(car3.drive());
20
21// class
22class ClassCar {
23 drive () {
24 console.log('Vroom!');
25 }
26}
27const car1 = new ClassCar();
28console.log(car1.drive());