-
Notifications
You must be signed in to change notification settings - Fork 0
/
creating objects.js
68 lines (53 loc) · 1.56 KB
/
creating objects.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
// Ways of creating Objects
// 1. object literal
let device1 = {
brand: "OnePlus",
model: "Nord",
price: 24999,
display: function () {
console.log(`Introducing the ${this.brand} ${this.model}`);
console.log(`Price : ₹${this.price}`);
}
};
// 2. Object() constructor
let device2 = new Object();
device2.brand = "Mi";
device2.model = "Power Bank 2i";
device2.price = 899;
device2.display = function () {
console.log(`Introducing the ${this.brand} ${this.model}`);
console.log(`Price : ₹${this.price}`);
}
// 3. passing an object literal to Object() constructor
let device3 = new Object({
brand: "EGATE",
model: "i9 Projector",
price: 5990,
display: function () {
console.log(`Introducing the ${this.brand} ${this.model}`);
console.log(`Price : ₹${this.price}`);
}
});
// 4. constructor function
function Device(brand, model, price) {
this.brand = brand;
this.model = model;
this.price = price;
this.display = function () {
console.log(`Introducing the ${this.brand} ${this.model}`);
console.log(`Price : ₹${this.price}`);
}
}
// instantitation - new instance of Device
let device4 = new Device("Apple", "Watch Series 3", 23900);
// 5. create() - create a new object based on an existing object
let device5 = Object.create(device3);
// 6. using an exisitng object's constructor property
let device6 = device5.constructor("Lenovo", "Tab3 Essential", "7000")
// uncomment to display devices
// device1.display();
// device2.display();
// device3.display();
// device4.display();
// device5.display();
// device6.display();