-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-JavaScriptObjects.js
More file actions
150 lines (125 loc) · 2.92 KB
/
08-JavaScriptObjects.js
File metadata and controls
150 lines (125 loc) · 2.92 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//JavaScript Objects
const car = {
name: "BMW",
model: 500,
weight: "850kg",
color: "white",
start: function () {
console.log("Car has started");
},
drive: function () {
console.log("Car is driving.");
},
brack: function () {
console.log("The car is bracket.");
},
stop: function () {
console.log("The car is stop.");
},
fullName: function () {
return this.name + " " + this.model;
},
};
const parson = {
fastName: "Sahin",
lastName: "Enam",
age: 17,
home: "Bhola",
fullName: function () {
return this.fastName + " " + this.lastName;
},
};
console.log(car.weight);
console.log(car.color);
console.log(car['model']);
car.start();
car.brack();
car.stop();
console.log(car.fullName());
console.log(parson.fullName());
// JavaScript Objects
/*
* Object Literal
* If you create a JavaScript Object using literals then its not a Singleton
* Constractor can create Singleton
*/
// Explain Symbol
const newSymb = Symbol('Key1');
// Object Literal
const mobileModel = {
// key: value
brand: 'Samsung',
model: 's24 Ultra',
processor: 'Sanpdragon Gen 3',
camera: ['200MP', '12MP', '12MP'],
hasZoomCamera: true,
// To Explain Bracket Notation
'selfie camera MP': 12,
// Let me use symbol as keys in Object
[newSymb]: 'MyKey1',
brandModel: function () {
return `Mobile Brand is ${this.brand} and model is ${this.model}`;
},
battray: {
mah: 5000,
},
};
/*
* Object Access Process
* You can do this two Ways
* dot Notation & Bracket Notation
*/
// Dot Notation
console.log(mobileModel.brand);
// Bracket Notation
console.log(mobileModel['precessor']);
console.log(mobileModel['selfie camera']);
console.log(mobileModel[newSymb]); // No other option then bracket notation
// Change Oblect values
mobileModel.model = 's25 Ultra';
// You can freez an Object
Object.freeze(mobileModel);
mobileModel.model = 's26 Ultra';
// Sometimes you might need to collect all keys as arrays
console.log(Object.keys(mobileModel));
// If need values
console.log(Object.values(mobileModel));
//If need to search a key is available or not
console.log(mobileModel.hasOwnProperty('camera'));
console.log(mobileModel.brandModel());
const obj1 = {
a: 1,
b: 2,
c: 3,
};
const obj2 = {
p: 1,
q: 2,
r: 3,
};
const obj3 = {
x: 1,
y: 2,
z: 3,
};
// const obj3 = { obj1, obj2 };
// const objFinal = Object.assign(target, source);
// const objFinal = Object.assign({}, obj1, obj2, obj3);
const objFinal = { ...obj1, ...obj2, ...obj3 };
console.log(objFinal);
console.log(mobileModel.battray.mah);
/*
* Object Constractor
* Singleton
*/
function Person(first, last) {
(this.firstName = first), (this.lastName = last);
}
const person1 = new Person('Ali', 'Hossain');
person1.age = 18;
const person2 = new Person('Shovo', 'Ahmed');
person2.greet = function () {
return 'Hello';
};
console.log(person1);
console.log(person2.greet());