-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype-pattern.js
More file actions
110 lines (86 loc) · 2.55 KB
/
prototype-pattern.js
File metadata and controls
110 lines (86 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* CH. 02 - Core JavaScript Object Creation & Design Patterns
* Prototype Pattern
*
* JavaScript Patterns - modern JS patterns with ES5 & ES6 examples
* @copyright 2016 - 2017, Ahad Bokhari
* license: MIT
*/
/* (i.) The prototype pattern focuses on creating an object that can be used as a blueprint for other
* objects through prototypal inheritance. The prototype pattern is an easy way to implement
* inheritance, and comes with a performance boost as well (all child objects create a reference to
* the function)
*/
// a. A simple implementation of the prototype pattern using object.create
var myBluePrint = {
name: "Blue Print",
getName: function() {
console.log('My name is ' + this.name);
}
};
var blueObject = Object.create(myBluePrint);
// a prototype is created
console.log(blueObject.name);
// => "Blue Print"
// b. By using the second argument of object.create we can easily create objects that inherit from on
// another
var aBluePrint = {
name: "Blue Print",
getName: function() {
console.log('My name is ' + this.name);
}
};
var aBlueObject = Object.create(myBluePrint, {
"id": {
value: Math.floor(Math.random() * 45),
writable: true,
enumerable: true,
configurable: false
},
"name": {
value: "Matt Lowenowski",
configurable: false
}
});
// c. Here is another basic implementation, which works great for applications that focus on
// object creation
var MyBase = function MyBaseObject() {
this.someFunction = function someFunction() {
console.log('Some function');
};
this.someOtherFunction = function someOtherFunction() {
console.log('Some other function');
};
this.showMyName = function showMyName() {
console.log(this.name)
};
};
function MyObject() {
this.name = 'Testing showMyName()';
}
MyObject.prototype = new MyBase();
// example usage
var testObj = new MyObject();
testObj.someFunction(); //=> 'Some function'
testObj.someOtherFunction(); //=> 'Some other function'
testObj.showMyName(); // alerts "Testing showMyName()"
// d. It's worth noting that it's possible to implement the prototype pattern without
// directly using object.create
var humanPrototype = {
init: function (religion) {
this.religion = religion;
},
getReligion: function () {
console.log("The religion of this person is " + " " + this.religion);
}
};
function person(religion) {
function R() {}
R.prototype = humanPrototype;
var r = new R();
r.init(religion);
return r;
}
var ayesha = person("Muslim");
ayesha.getReligion();
//=> "The religion of this person is Muslim"