forked from codesONLY/JavaScriptONLY
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.js
More file actions
35 lines (26 loc) · 750 Bytes
/
Copy pathprototype.js
File metadata and controls
35 lines (26 loc) · 750 Bytes
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
let animal = {
eats: true,
};
function Rabbit(name) {
this.name = name;
}
Rabbit.prototype = animal;
let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal
let rabbit2 = new rabbit.constructor("Black Rabbit"); // another way to create a new object
// re-assigning prototype of Rabbit
Rabbit.prototype = {
jumps: true,
};
let rabbit3 = new Rabbit();
console.log(rabbit.constructor === Rabbit); // false
// Not overwrite Rabbit.prototype totally
// just add to it
Rabbit.prototype.jumps = true;
// the default Rabbit.prototype.constructor is preserved
let rabbit4 = new Rabbit();
console.log(rabbit.constructor === Rabbit); // false
// Other alternative way
Rabbit.prototype = {
jumps: true,
constructor: Rabbit,
};