-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_changePrototype2NewObject.js
More file actions
46 lines (39 loc) · 1.09 KB
/
13_changePrototype2NewObject.js
File metadata and controls
46 lines (39 loc) · 1.09 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
// Change the Prototype to a New Object
/* Adding prototype properties individually is tedious.
Avoid the tedium by creating a prototype object */
function Bird(name) {
this.name = name;
}
Bird.prototype = {
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
}
let roadrunner = new Bird("Road Runner");
console.log(roadrunner.numLegs); // 2
roadrunner.eat(); // nom nom nom
roadrunner.describe(); // My name is Road Runner
/* Add the property numLegs and the two methods eat() and
describe() to the prototype of Dog by setting the prototype
to a new object. */
function Dog(name) {
this.name = name;
}
Dog.prototype = {
// Only change code below this line
numLegs: 4,
eat: function () {
console.log("nom nom nom");
},
describe: function () {
console.log("My name is " + this.name + ".");
}
};
let jackRussel = new Dog("Jack");
console.log(jackRussel.numLegs); // 4
jackRussel.eat(); // nom nom nom
jackRussel.describe(); // My name is Jack.