Skip to content

Commit fa1caed

Browse files
committed
added documentation and reference to code reuse patterns
1 parent 98045e1 commit fa1caed

10 files changed

+125
-12
lines changed

code-reuse-patterns/borrowing-methods.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<body>
88
<script>
99
/* Title: Borrowing Methods
10-
Description: generally a pattern that should be avoided unless one is more comfortable with class than prototype
10+
Description: reuse one or two methods of an existing object without forming a parent-child relationship with that object
1111
*/
1212

1313
function f() {
@@ -38,7 +38,7 @@
3838

3939
// passing as a callback
4040
var yetanother = {
41-
name: "Yet another object",
41+
name: 'Yet another object',
4242
method: function (callback) {
4343
return callback('Hola');
4444
}
@@ -55,6 +55,8 @@
5555
console.log(twosay('yo')); // "yo, another object"
5656

5757

58+
// ECMAScript 5 adds a method bind() to Function.prototype, making it just as easy to use as apply() and call().
59+
5860
if (typeof Function.prototype.bind === 'undefined') {
5961
Function.prototype.bind = function (thisArg) {
6062
var fn = this,
@@ -71,6 +73,10 @@
7173

7274
var twosay3 = one.say.bind(two, 'Enchanté');
7375
console.log(twosay3()); // "Enchanté, another object"
76+
77+
78+
// reference
79+
// http://shop.oreilly.com/product/9780596806767.do
7480
</script>
7581
</body>
7682
</html>

code-reuse-patterns/cp1-default.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
// Drawback 2: it doesn't enable you to pass parameters to the child constructor
4141
var s = new Child('Seth');
4242
console.log(s.say()); // "Adam"
43+
44+
45+
// reference
46+
// http://shop.oreilly.com/product/9780596806767.do
4347
</script>
4448
</body>
4549
</html>

code-reuse-patterns/cp2-rent-a-constructor.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151

5252
var jane = new CatWings();
5353
console.dir(jane);
54+
55+
56+
// reference
57+
// http://shop.oreilly.com/product/9780596806767.do
5458
</script>
5559
</body>
5660
</html>

code-reuse-patterns/cp3-rent-and-set-prototype.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
console.log(kid.say()); // "Adam"
3737

3838
// Drawback - the parent constructor is called twice, so it could be inefficient
39+
40+
41+
// reference
42+
// http://shop.oreilly.com/product/9780596806767.do
3943
</script>
4044
</body>
4145
</html>

code-reuse-patterns/cp4-share-the-prototype.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
kid.name = 'Patrick';
3636
console.log(kid.say()); // Patrick
3737
console.dir(kid);
38+
39+
40+
// reference
41+
// http://shop.oreilly.com/product/9780596806767.do
3842
</script>
3943
</body>
4044
</html>

code-reuse-patterns/cp5-a-temporary-constructor.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@
6666
console.log(kid.say()); // Patrick
6767
console.log(kid.constructor.name); // Child
6868
console.log(kid.constructor === Parent); // false
69-
69+
70+
71+
// reference
72+
// http://shop.oreilly.com/product/9780596806767.do
7073
</script>
7174
</body>
7275
</html>

code-reuse-patterns/inheritance-by-copying-properties.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969

7070
console.log(dad.reads === kid.reads); // false
7171
kid.reads.paper = false;
72+
73+
74+
// reference
75+
// http://shop.oreilly.com/product/9780596806767.do
7276
</script>
7377
</body>
7478
</html>

code-reuse-patterns/klass.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,81 @@
7878

7979
console.log(clark instanceof Man); // true
8080
console.log(clark instanceof SuperMan); // true
81+
<!doctype html>
82+
<html lang="en">
83+
<head>
84+
<title>JavaScript Patterns</title>
85+
<meta charset="utf-8">
86+
</head>
87+
<body>
88+
<script>
89+
/* Title: Classical Pattern #5 - A Temporary Constructor (a pattern that should be generally avoided)
90+
Description: first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
91+
*/
92+
93+
/* Basic */
94+
/*function inherit(C, P) {
95+
var F = function () {};
96+
F.prototype = P.prototype;
97+
C.prototype = new F();
98+
}*/
99+
100+
/* Storing the Superclass */
101+
/*function inherit(C, P) {
102+
var F = function () {};
103+
F.prototype = P.prototype;
104+
C.prototype = new F();
105+
C.uber = P.prototype;
106+
}*/
107+
108+
/* Resetting the Constructor Pointer */
109+
/*function inherit(C, P) {
110+
var F = function () {};
111+
F.prototype = P.prototype;
112+
C.prototype = new F();
113+
C.uber = P.prototype;
114+
C.prototype.constructor = C;
115+
}*/
116+
117+
/* in closure */
118+
var inherit = (function () {
119+
var F = function () {};
120+
return function (C, P) {
121+
F.prototype = P.prototype;
122+
C.prototype = new F();
123+
C.uber = P.prototype;
124+
C.prototype.constructor = C;
125+
}
126+
}());
127+
128+
function Parent(name) {
129+
this.name = name || 'Adam';
130+
}
131+
132+
// adding functionality to the prototype
133+
Parent.prototype.say = function () {
134+
return this.name;
135+
};
136+
137+
// child constructor
138+
function Child(name) {}
139+
140+
inherit(Child, Parent);
141+
142+
var kid = new Child();
143+
console.log(kid.name); // undefined
144+
console.log(typeof kid.say); // function
145+
kid.name = 'Patrick';
146+
console.log(kid.say()); // Patrick
147+
console.log(kid.constructor.name); // Child
148+
console.log(kid.constructor === Parent); // false
149+
150+
151+
// reference
152+
// http://shop.oreilly.com/product/9780596806767.do
153+
</script>
154+
</body>
155+
</html>
81156
</script>
82157
</body>
83158
</html>

code-reuse-patterns/mix-ins.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<body>
88
<script>
99
/* Title: Mix-ins
10-
Description: generally a pattern that should be avoided unless one is more comfortable with class than prototype
10+
Description: copy from any number of objects and mix them all into a new object
1111
*/
1212

1313
function mix() {
@@ -25,11 +25,16 @@
2525
var cake = mix(
2626
{eggs: 2, large: true},
2727
{butter: 1, salted: true},
28-
{flour: "3 cups"},
29-
{sugar: "sure!"}
28+
{flour: '3 cups'},
29+
{sugar: 'sure!'}
3030
);
3131

3232
console.dir(cake);
33+
34+
35+
// reference
36+
// http://addyosmani.com/resources/essentialjsdesignpatterns/book/#mixinpatternjavascript
37+
// http://shop.oreilly.com/product/9780596806767.do
3338
</script>
3439
</body>
3540
</html>

code-reuse-patterns/prototypal-inheritance.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
var child = object(parent);
2626

2727
// testing
28-
alert(child.name); // "Papa"
28+
console.log(child.name); // "Papa"
2929

3030

3131
// parent constructor
@@ -43,7 +43,7 @@
4343
var kid = object(papa);
4444
// test that both the own property
4545
// and the prototype property were inherited
46-
kid.getName(); // "Adam"
46+
console.log(kid.getName()); // "Adam"
4747

4848

4949
// parent constructor
@@ -57,17 +57,21 @@
5757
};
5858
// inherit
5959
var kid = object(Person.prototype);
60-
typeof kid.getName; // "function", because it was in the prototype
61-
typeof kid.name; // "undefined", because only the prototype was inherited
60+
console.log(typeof kid.getName); // "function", because it was in the prototype
61+
console.log(typeof kid.name); // "undefined", because only the prototype was inherited
6262

6363

6464
/* Addition to ECMAScript 5 */
6565
var child = Object.create(parent);
6666

6767
var child = Object.create(parent, {
68-
age: { value: 2 } // ECMA5 descriptor
68+
age: { value: 2 } // ECMA5 descriptor
6969
});
70-
child.hasOwnProperty("age"); // true
70+
console.log(child.hasOwnProperty("age")); // true
71+
72+
73+
// reference
74+
// http://shop.oreilly.com/product/9780596806767.do
7175
</script>
7276
</body>
7377
</html>

0 commit comments

Comments
 (0)