Skip to content

Commit 4e16508

Browse files
committed
adding code for Chapter 13
1 parent 9ce0dfa commit 4e16508

16 files changed

+980
-0
lines changed

chapter13/cliche.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Extending the String prototype with a new method</title>
6+
<script>
7+
8+
String.prototype.cliche= function() {
9+
var cliche = ["lock and load","touch base", "open the kimono"];
10+
11+
for (var i = 0; i < cliche.length; i++) {
12+
var index = this.indexOf(cliche[i]);
13+
if (index >= 0) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
};
19+
20+
var sentences = ["I'll send my car around to pick you up.",
21+
"Let's touch base in the morning and see where we are",
22+
"We don't want to open the kimono, we just want to inform them."];
23+
24+
for (var i = 0; i < sentences.length; i++) {
25+
var phrase = sentences[i];
26+
if (phrase.cliche()) {
27+
console.log("CLICHE ALERT: " + phrase);
28+
}
29+
}
30+
31+
32+
</script>
33+
</head>
34+
<body>
35+
</body>
36+
</html>
37+

chapter13/dog.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Fido</title>
5+
<meta charset="utf-8">
6+
<script>
7+
function Dog(name, breed, weight) {
8+
this.name = name;
9+
this.breed = breed;
10+
this.weight = weight;
11+
}
12+
var fido = new Dog("Fido", "Mixed", 38);
13+
var fluffy = new Dog("Fluffy", "Poodle", 30);
14+
var spot = new Dog("Spot", "Chihuahua", 10);
15+
var dogs = [fido, fluffy, spot];
16+
17+
for (var i = 0; i < dogs.length; i++) {
18+
var size = "small";
19+
if (dogs[i].weight > 10) {
20+
size = "large";
21+
}
22+
console.log("Dog: " + dogs[i].name
23+
+ " is a " + size
24+
+ " " + dogs[i].breed);
25+
}
26+
</script>
27+
</head>
28+
<body>
29+
</body>
30+
</html>
31+

chapter13/dog1.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Fido with Prototype</title>
6+
<script>
7+
function Dog(name, breed, weight) {
8+
this.name = name;
9+
this.breed = breed;
10+
this.weight = weight;
11+
}
12+
13+
Dog.prototype.species = "Canine";
14+
15+
Dog.prototype.bark = function() {
16+
if (this.weight > 25) {
17+
console.log(this.name + " says Woof!");
18+
} else {
19+
console.log(this.name + " says Yip!");
20+
}
21+
};
22+
23+
Dog.prototype.run = function() {
24+
console.log("Run!");
25+
};
26+
Dog.prototype.wag = function() {
27+
console.log("Wag!");
28+
};
29+
30+
var fido = new Dog("Fido", "Mixed", 38);
31+
var fluffy = new Dog("Fluffy", "Poodle", 30);
32+
var spot = new Dog("Spot", "Chihuahua", 10);
33+
34+
fido.bark();
35+
fido.run();
36+
fido.wag();
37+
38+
fluffy.bark();
39+
fluffy.run();
40+
fluffy.wag();
41+
42+
spot.bark();
43+
spot.run();
44+
spot.wag();
45+
46+
</script>
47+
</head>
48+
<body>
49+
</body>
50+
</html>
51+

chapter13/dog2.html

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Spot gets his WOOF!</title>
6+
<script>
7+
function Dog(name, breed, weight) {
8+
this.name = name;
9+
this.breed = breed;
10+
this.weight = weight;
11+
}
12+
13+
Dog.prototype.species = "Canine";
14+
15+
Dog.prototype.bark = function() {
16+
if (this.weight > 25) {
17+
console.log(this.name + " says Woof!");
18+
} else {
19+
console.log(this.name + " says Yip!");
20+
}
21+
};
22+
23+
Dog.prototype.run = function() {
24+
console.log("Run!");
25+
};
26+
Dog.prototype.wag = function() {
27+
console.log("Wag!");
28+
};
29+
30+
var fido = new Dog("Fido", "Mixed", 38);
31+
var fluffy = new Dog("Fluffy", "Poodle", 30);
32+
var spot = new Dog("Spot", "Chihuahua", 10);
33+
34+
spot.bark = function() {
35+
console.log(this.name + " says WOOF!");
36+
};
37+
38+
fido.bark();
39+
fido.run();
40+
fido.wag();
41+
42+
fluffy.bark();
43+
fluffy.run();
44+
fluffy.wag();
45+
46+
spot.bark();
47+
spot.run();
48+
spot.wag();
49+
50+
</script>
51+
</head>
52+
<body>
53+
</body>
54+
</html>
55+

chapter13/dog3.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Barnaby gets to sit</title>
6+
<script>
7+
function Dog(name, breed, weight) {
8+
this.name = name;
9+
this.breed = breed;
10+
this.weight = weight;
11+
}
12+
13+
Dog.prototype.species = "Canine";
14+
15+
Dog.prototype.bark = function() {
16+
if (this.weight > 25) {
17+
console.log(this.name + " says Woof!");
18+
} else {
19+
console.log(this.name + " says Yip!");
20+
}
21+
};
22+
23+
Dog.prototype.run = function() {
24+
console.log("Run!");
25+
};
26+
Dog.prototype.wag = function() {
27+
console.log("Wag!");
28+
};
29+
30+
var fido = new Dog("Fido", "Mixed", 38);
31+
var fluffy = new Dog("Fluffy", "Poodle", 30);
32+
var spot = new Dog("Spot", "Chihuahua", 10);
33+
34+
spot.bark = function() {
35+
console.log(this.name + " says WOOF!");
36+
};
37+
38+
fido.bark();
39+
fido.run();
40+
fido.wag();
41+
42+
fluffy.bark();
43+
fluffy.run();
44+
fluffy.wag();
45+
46+
spot.bark();
47+
spot.run();
48+
spot.wag();
49+
50+
var barnaby = new Dog("Barnaby", "Basset Hound", 55);
51+
Dog.prototype.sit = function() {
52+
console.log(this.name + " is now sitting");
53+
}
54+
barnaby.sit();
55+
56+
</script>
57+
</head>
58+
<body>
59+
</body>
60+
</html>
61+

chapter13/dog4.html

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Adding sitting test</title>
6+
<script>
7+
function Dog(name, breed, weight) {
8+
this.name = name;
9+
this.breed = breed;
10+
this.weight = weight;
11+
}
12+
13+
Dog.prototype.species = "Canine";
14+
Dog.prototype.sitting = false;
15+
16+
Dog.prototype.bark = function() {
17+
if (this.weight > 25) {
18+
console.log(this.name + " says Woof!");
19+
} else {
20+
console.log(this.name + " says Yip!");
21+
}
22+
};
23+
24+
Dog.prototype.run = function() {
25+
console.log("Run!");
26+
};
27+
Dog.prototype.wag = function() {
28+
console.log("Wag!");
29+
};
30+
Dog.prototype.sit = function() {
31+
if (this.sitting) {
32+
console.log(this.name + " is already sitting");
33+
} else {
34+
this.sitting = true;
35+
console.log(this.name + " is now sitting");
36+
}
37+
};
38+
39+
var fido = new Dog("Fido", "Mixed", 38);
40+
var fluffy = new Dog("Fluffy", "Poodle", 30);
41+
var spot = new Dog("Spot", "Chihuahua", 10);
42+
var barnaby = new Dog("Barnaby", "Basset Hound", 55);
43+
44+
spot.bark = function() {
45+
console.log(this.name + " says WOOF!");
46+
};
47+
48+
fido.bark();
49+
fido.run();
50+
fido.wag();
51+
52+
fluffy.bark();
53+
fluffy.run();
54+
fluffy.wag();
55+
56+
spot.bark();
57+
spot.run();
58+
spot.wag();
59+
60+
barnaby.sit();
61+
barnaby.sit();
62+
spot.sit();
63+
spot.sit();
64+
65+
</script>
66+
</head>
67+
<body>
68+
</body>
69+
</html>
70+

chapter13/dog5.html

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Who has the sitting property?</title>
6+
<script>
7+
function Dog(name, breed, weight) {
8+
this.name = name;
9+
this.breed = breed;
10+
this.weight = weight;
11+
}
12+
13+
Dog.prototype.species = "Canine";
14+
Dog.prototype.sitting = false;
15+
16+
Dog.prototype.bark = function() {
17+
if (this.weight > 25) {
18+
console.log(this.name + " says Woof!");
19+
} else {
20+
console.log(this.name + " says Yip!");
21+
}
22+
};
23+
24+
Dog.prototype.run = function() {
25+
console.log("Run!");
26+
};
27+
Dog.prototype.wag = function() {
28+
console.log("Wag!");
29+
};
30+
Dog.prototype.sit = function() {
31+
if (this.sitting) {
32+
console.log(this.name + " is already sitting");
33+
} else {
34+
this.sitting = true;
35+
console.log(this.name + " is now sitting");
36+
}
37+
};
38+
39+
var fido = new Dog("Fido", "Mixed", 38);
40+
var fluffy = new Dog("Fluffy", "Poodle", 30);
41+
var spot = new Dog("Spot", "Chihuahua", 10);
42+
var barnaby = new Dog("Barnaby", "Basset Hound", 55);
43+
44+
spot.bark = function() {
45+
console.log(this.name + " says WOOF!");
46+
};
47+
48+
fido.bark();
49+
fido.run();
50+
fido.wag();
51+
52+
fluffy.bark();
53+
fluffy.run();
54+
fluffy.wag();
55+
56+
spot.bark();
57+
spot.run();
58+
spot.wag();
59+
60+
barnaby.sit();
61+
barnaby.sit();
62+
spot.sit();
63+
spot.sit();
64+
65+
console.log("Does spot have a sitting property? " + spot.hasOwnProperty("sitting"));
66+
console.log("Does fido have a sitting property? " + fido.hasOwnProperty("sitting"));
67+
68+
</script>
69+
</head>
70+
<body>
71+
</body>
72+
</html>
73+

0 commit comments

Comments
 (0)