Skip to content

Commit f1b4f9a

Browse files
Lars KappertLars Kappert
Lars Kappert
authored and
Lars Kappert
committed
Proper indentation using tabs (both HTML and JS)
1 parent 8de6643 commit f1b4f9a

File tree

77 files changed

+3875
-3800
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+3875
-3800
lines changed
+79-79
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<title>JavaScript Patterns</title>
5-
<meta charset="utf-8">
6-
</head>
7-
<body>
8-
<script>
9-
/* Title: Borrowing Methods
10-
Description: reuse one or two methods of an existing object without forming a parent-child relationship with that object
11-
*/
12-
13-
function f() {
14-
var args = [].slice.call(arguments, 1, 3);
15-
return args;
16-
}
17-
18-
var one = {
19-
name: 'object',
20-
say: function (greet) {
21-
return greet + ', ' + this.name;
22-
}
23-
};
24-
25-
// test
26-
console.log(one.say('hi')); // "hi, object"
27-
28-
var two = {
29-
name: 'another object'
30-
};
31-
32-
console.log(one.say.apply(two, ['hello'])); // "hello, another object"
33-
34-
// assigning to a variable
35-
// `this` will point to the global object
36-
var say = one.say;
37-
console.log(say('hoho')); // "hoho, undefined"
38-
39-
// passing as a callback
40-
var yetanother = {
41-
name: 'Yet another object',
42-
method: function (callback) {
43-
return callback('Hola');
44-
}
45-
};
46-
console.log(yetanother.method(one.say)); // "Holla, undefined"
47-
48-
function bind(o, m) {
49-
return function () {
50-
return m.apply(o, [].slice.call(arguments));
51-
};
52-
}
53-
54-
var twosay = bind(two, one.say);
55-
console.log(twosay('yo')); // "yo, another object"
56-
57-
58-
// ECMAScript 5 adds a method bind() to Function.prototype, making it just as easy to use as apply() and call().
59-
60-
if (typeof Function.prototype.bind === 'undefined') {
61-
Function.prototype.bind = function (thisArg) {
62-
var fn = this,
63-
slice = Array.prototype.slice,
64-
args = slice.call(arguments, 1);
65-
return function () {
66-
return fn.apply(thisArg, args.concat(slice.call(arguments)));
67-
};
68-
};
69-
}
70-
71-
var twosay2 = one.say.bind(two);
72-
console.log(twosay2('Bonjour')); // "Bonjour, another object"
73-
74-
var twosay3 = one.say.bind(two, 'Enchanté');
75-
console.log(twosay3()); // "Enchanté, another object"
76-
77-
78-
// reference
79-
// http://shop.oreilly.com/product/9780596806767.do
80-
</script>
81-
</body>
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/* Title: Borrowing Methods
10+
Description: reuse one or two methods of an existing object without forming a parent-child relationship with that object
11+
*/
12+
13+
function f() {
14+
var args = [].slice.call(arguments, 1, 3);
15+
return args;
16+
}
17+
18+
var one = {
19+
name:'object',
20+
say:function (greet) {
21+
return greet + ', ' + this.name;
22+
}
23+
};
24+
25+
// test
26+
console.log(one.say('hi')); // "hi, object"
27+
28+
var two = {
29+
name:'another object'
30+
};
31+
32+
console.log(one.say.apply(two, ['hello'])); // "hello, another object"
33+
34+
// assigning to a variable
35+
// `this` will point to the global object
36+
var say = one.say;
37+
console.log(say('hoho')); // "hoho, undefined"
38+
39+
// passing as a callback
40+
var yetanother = {
41+
name:'Yet another object',
42+
method:function (callback) {
43+
return callback('Hola');
44+
}
45+
};
46+
console.log(yetanother.method(one.say)); // "Holla, undefined"
47+
48+
function bind(o, m) {
49+
return function () {
50+
return m.apply(o, [].slice.call(arguments));
51+
};
52+
}
53+
54+
var twosay = bind(two, one.say);
55+
console.log(twosay('yo')); // "yo, another object"
56+
57+
58+
// ECMAScript 5 adds a method bind() to Function.prototype, making it just as easy to use as apply() and call().
59+
60+
if (typeof Function.prototype.bind === 'undefined') {
61+
Function.prototype.bind = function (thisArg) {
62+
var fn = this,
63+
slice = Array.prototype.slice,
64+
args = slice.call(arguments, 1);
65+
return function () {
66+
return fn.apply(thisArg, args.concat(slice.call(arguments)));
67+
};
68+
};
69+
}
70+
71+
var twosay2 = one.say.bind(two);
72+
console.log(twosay2('Bonjour')); // "Bonjour, another object"
73+
74+
var twosay3 = one.say.bind(two, 'Enchanté');
75+
console.log(twosay3()); // "Enchanté, another object"
76+
77+
78+
// reference
79+
// http://shop.oreilly.com/product/9780596806767.do
80+
</script>
81+
</body>
8282
</html>

code-reuse-patterns/cp1-default.html

+47-46
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<title>JavaScript Patterns</title>
5-
<meta charset="utf-8">
6-
</head>
7-
<body>
8-
<script>
9-
/* Title: Classical Pattern #1 - The Default Pattern (a pattern that should be generally avoided)
10-
Description: create an object using the Parent() constructor and assign this object to the Child()'s prototype
11-
*/
12-
13-
function inherit(C, P) {
14-
C.prototype = new P();
15-
}
16-
17-
// the parent constructor
18-
function Parent(name) {
19-
this.name = name || 'Adam';
20-
}
21-
// adding functionality to the prototype
22-
Parent.prototype.say = function () {
23-
return this.name;
24-
};
25-
// empty child constructor
26-
function Child(name) {}
27-
28-
// inheritance magic happens here
29-
inherit(Child, Parent);
30-
31-
var kid = new Child();
32-
console.log(kid.say()); // "Adam"
33-
34-
// Drawback 1: own properties added to `this` is inherited
35-
var kiddo = new Child();
36-
kiddo.name = "Patrick";
37-
console.log(kiddo.say()); // "Patrick"
38-
39-
40-
// Drawback 2: it doesn't enable you to pass parameters to the child constructor
41-
var s = new Child('Seth');
42-
console.log(s.say()); // "Adam"
43-
44-
45-
// reference
46-
// http://shop.oreilly.com/product/9780596806767.do
47-
</script>
48-
</body>
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/* Title: Classical Pattern #1 - The Default Pattern (a pattern that should be generally avoided)
10+
Description: create an object using the Parent() constructor and assign this object to the Child()'s prototype
11+
*/
12+
13+
function inherit(C, P) {
14+
C.prototype = new P();
15+
}
16+
17+
// the parent constructor
18+
function Parent(name) {
19+
this.name = name || 'Adam';
20+
}
21+
// adding functionality to the prototype
22+
Parent.prototype.say = function () {
23+
return this.name;
24+
};
25+
// empty child constructor
26+
function Child(name) {
27+
}
28+
29+
// inheritance magic happens here
30+
inherit(Child, Parent);
31+
32+
var kid = new Child();
33+
console.log(kid.say()); // "Adam"
34+
35+
// Drawback 1: own properties added to `this` is inherited
36+
var kiddo = new Child();
37+
kiddo.name = "Patrick";
38+
console.log(kiddo.say()); // "Patrick"
39+
40+
41+
// Drawback 2: it doesn't enable you to pass parameters to the child constructor
42+
var s = new Child('Seth');
43+
console.log(s.say()); // "Adam"
44+
45+
46+
// reference
47+
// http://shop.oreilly.com/product/9780596806767.do
48+
</script>
49+
</body>
4950
</html>
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<title>JavaScript Patterns</title>
5-
<meta charset="utf-8">
6-
</head>
7-
<body>
8-
<script>
9-
/* Title: Classical Pattern #2 - Rent a Constructor (a pattern that should be generally avoided)
10-
Description: it borrows the parent constructor, passing the child object to be bound to this and also forwarding any arguments
11-
*/
12-
13-
// the parent constructor
14-
function Parent(name) {
15-
this.name = name || 'Adam';
16-
}
17-
18-
// adding functionality to the prototype
19-
Parent.prototype.say = function () {
20-
return this.name;
21-
};
22-
23-
// child constructor
24-
function Child(name) {
25-
Parent.apply(this, arguments);
26-
}
27-
28-
var kid = new Child("Patrick");
29-
console.log(kid.name); // "Patrick"
30-
31-
// Drawback 1: nothing from the prototype gets inherited
32-
console.log(typeof kid.say); // "undefined"
33-
34-
// Multiple Inheritance by Borrowing Constructors
35-
function Cat() {
36-
this.legs = 4;
37-
this.say = function () {
38-
return "meaowww";
39-
}
40-
}
41-
42-
function Bird() {
43-
this.wings = 2;
44-
this.fly = true;
45-
}
46-
47-
function CatWings() {
48-
Cat.apply(this);
49-
Bird.apply(this);
50-
}
51-
52-
var jane = new CatWings();
53-
console.dir(jane);
54-
55-
56-
// reference
57-
// http://shop.oreilly.com/product/9780596806767.do
58-
</script>
59-
</body>
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/* Title: Classical Pattern #2 - Rent a Constructor (a pattern that should be generally avoided)
10+
Description: it borrows the parent constructor, passing the child object to be bound to this and also forwarding any arguments
11+
*/
12+
13+
// the parent constructor
14+
function Parent(name) {
15+
this.name = name || 'Adam';
16+
}
17+
18+
// adding functionality to the prototype
19+
Parent.prototype.say = function () {
20+
return this.name;
21+
};
22+
23+
// child constructor
24+
function Child(name) {
25+
Parent.apply(this, arguments);
26+
}
27+
28+
var kid = new Child("Patrick");
29+
console.log(kid.name); // "Patrick"
30+
31+
// Drawback 1: nothing from the prototype gets inherited
32+
console.log(typeof kid.say); // "undefined"
33+
34+
// Multiple Inheritance by Borrowing Constructors
35+
function Cat() {
36+
this.legs = 4;
37+
this.say = function () {
38+
return "meaowww";
39+
}
40+
}
41+
42+
function Bird() {
43+
this.wings = 2;
44+
this.fly = true;
45+
}
46+
47+
function CatWings() {
48+
Cat.apply(this);
49+
Bird.apply(this);
50+
}
51+
52+
var jane = new CatWings();
53+
console.dir(jane);
54+
55+
56+
// reference
57+
// http://shop.oreilly.com/product/9780596806767.do
58+
</script>
59+
</body>
6060
</html>

0 commit comments

Comments
 (0)