-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype and constructor.js
100 lines (76 loc) · 2.43 KB
/
prototype and constructor.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// protoype and constructor
// Object Definition
// Style convention:
// Define properties on the constructor function &
// methods on the constructor's prototype property.
// a constructor function defining properties
function Device(brand, model, price, imgSrc) {
this.brand = brand;
this.model = model;
this.price = price;
this.imgSrc = imgSrc;
}
// Every constructor has a prototype property
// (a bucket for things to be inherited without copying)
console.log(
Device.prototype
);
// Adding methods by modifying the constructor's prototype property
Device.prototype.display = function () {
console.log(`Introducing the ${this.brand} ${this.model}`);
console.log(`Price : ₹${this.price}`);
}
// a new object using the constructor function
let device1 = new Device("Apple", "Watch Series 3", 23900);
// Object's prototype property
console.log(
Object.getPrototypeOf(device1)
); // or
console.log(
device1.__proto__
);
// Note: An object's prototype property refers to its constructor's prototype property.
console.log(
Object.getPrototypeOf(device1) === Device.prototype // -> true
); // or
console.log(
device1.__proto__ === Device.prototype // -> true
);
// Prototype Chaining
// a new object using another object as its prototype
let device2 = Object.create(device1);
// Note:
console.log(
device2.__proto__ === Device.prototype // -> false
);
console.log(
device2.__proto__ === device1 // -> true
);
console.log(
device2.__proto__.__proto__ === Device.prototype // -> true
);
// demonstration of prototype chain traversal
device2.display();
// .display(), being unavailable in device2,
// is searched in its protoype i.e. device1,
// which too lacks it, so it's further searched
// up the prototype chain until its finally
// found in the prototype of device1's
// constructor i.e.'Device()'
// Constructor property
console.log(
device1.constructor === Device// -> true
);
console.log(
device1.constructor.name // -> "Device"
);
// create a new object using an existing object's constructor property
let device3 = new device2.constructor("Lenovo", "Tab3 Essential", "7000", ""); // equivalent to 'new Device(...)'
// as in the case of .display() above, here
// device2 doesn't contain a 'constructor' property
// but device1 has it, so the prototype chain is
// traversed upwards to access it.
// uncomment to display
// device1.display();
// device2.display();
// device3.display();