forked from EQuimper/All-About-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
Books/JavaScript/The principles of object-oriented javascript/notes.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## The Principles of Object-Oriented JavaScript | ||
|
||
|
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
72 changes: 72 additions & 0 deletions
72
...ct creation patterns tutorial - factory , constructor pattern, prototype pattern/index.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Factory Pattern | ||
*/ | ||
const peopleFactory = function(name, age, location) { | ||
const temp = {}; | ||
|
||
temp.name = name; | ||
temp.age = age; | ||
temp.location = location; | ||
|
||
temp.printPeople = function() { | ||
return console.log(`Hello ${this.name}, ${this.age} who came from ${this.location}`); | ||
} | ||
|
||
return temp; | ||
} | ||
|
||
const me = peopleFactory('Bob', 20, 'Canada'); | ||
|
||
me.printPeople() | ||
|
||
/** | ||
* Constructor Patter | ||
*/ | ||
const personConstructor = function(name, age, location) { | ||
this.name = name; | ||
this.age = age; | ||
this.location = location; | ||
|
||
this.printPerson = function() { | ||
return console.log`Hello ${this.name}, ${this.age} who came from ${this.location}`; | ||
} | ||
} | ||
|
||
const you = new personConstructor('Luc', 25, 'USA'); | ||
|
||
/** | ||
* Prototype Pattern | ||
*/ | ||
const peopleProto = function(){}; | ||
|
||
peopleProto.prototype.name = 'no name'; | ||
peopleProto.prototype.age = 0; | ||
peopleProto.prototype.city = 'no city'; | ||
peopleProto.prototype.print = function() { | ||
return console.log(`Hello ${this.name}, ${this.age} who came from ${this.city}`); | ||
} | ||
|
||
const people1 = new peopleProto(); | ||
|
||
people1.name = 'Yan'; | ||
people1.age = 30; | ||
people1.city = 'Quebec'; | ||
|
||
console.log(people1); | ||
|
||
console.log('name' in people1); // true | ||
|
||
/** | ||
* Dynamic Proto | ||
*/ | ||
const peopleDynamicProto = function(name, age, city) { | ||
this.name = name; | ||
this.age = age; | ||
this.city = city; | ||
|
||
if (typeof this.printPeople !== 'function') { | ||
peopleDynamicProto.prototype.print = function() { | ||
return console.log(`Hello ${this.name}, ${this.age} who came from ${this.city}`); | ||
}; | ||
} | ||
} |