-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathObjectPropertyMethod.js
171 lines (134 loc) · 4.17 KB
/
ObjectPropertyMethod.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const student = {
name: "Vishal",
college: "LPU",
id: 11607932,
"no of subject": 4,
};
// Accessing of the value
console.log(student.name); // Vishal
student["no of subject"]; //4
// deleting the key value from object (dynamically)
delete student.name;
console.log(student);
// adding the key and value in js object dynamically
student.firstName = "Vishal";
console.log(student);
// Shorthand Property
// important
// In js object if you pass variable inside it , in that case js object will consider that
// varibale as key and it the value which is obtained by variable will become a value of that key
const firstName = "Vishal";
const data = 40;
const obj1 = { firstName, data };
console.log("Shorthan Property::", obj1);
// In js let var const function return they are reserved keyword you cannot use this reserver keyword in any variable
// but in case of object this does impact
// let let = 30; this will give you error
// important
const student1 = {
const: 30,
let: "50",
function: "hey i am function",
return: 34,
};
console.log("Resreved keyword of js as key::", student1);
// we can add function inside the object as well
// function getIncrementSalary {
// return (this.salary * this.increment) / 100 + this.salary;
// }
// important
const employee = {
firstName: "Chim",
lastName: "Rim",
getFullName: function () {
console.log("this", this); // employee
return this.firstName + " " + this.lastName;
},
salary: 300000,
increment: 30,
getIncrementSalary: function () {
return (this.salary * this.increment) / 100 + this.salary;
},
getIncrement: () => {
console.log("this inside arrow function", this);
return this.increment;
},
};
// if you are tring to access this inside normal function insdie object than this will; point to the that object itself
// if you are tring to access this inside arrow function inside object than this will not point to the that object this will point out window object in this case
console.log(employee.getFullName, "getFullName");
console.log(employee.getFullName(), "getFullName call");
console.log(employee.getIncrementSalary());
console.log(employee.getIncrement());
// Destructring
// {} one empyt object
const data123 = {}; // one empty
const sum = (a, b) => {
return a + b;
};
sum; // this simple variable
sum(1, 2); // invoked functions
// Getters and setters in Javascript Object
// this are like only function
const vehicle = {
type: "Car",
tyre: 4,
// this is called a getter for tyre
get getTyre() {
// if (this.tyre > 4) {
// return "car";
// } else {
// return "bike";
// }
return this.tyre;
},
// setter for tyre
set setTyre(count) {
// you need to go through sanitization
// if validation true than upodate else return error
if (isNaN(count)) {
this.tyre = 4;
} else {
this.tyre = count;
}
},
};
console.log("gettters", vehicle.tyre);
vehicle.setTyre = 6;
// vehicle.tyre = "chim";
vehicle.setTyre = "chim";
console.log("gettters", vehicle.getTyre); // no need to invoked getter function directly
// const humane = {
// place: "earth",
// };
// humane.place = "Moon";
// object.create vs object.assign
// Object.create is used to create new object
// single level
const user = {
name: "Vishal",
id: "64wsdcsbssdd",
array: [1, 2, 3, 4, 5],
};
// passing null
const obj12 = Object.create(null); // this will create new object
obj12.name = "Vishal";
console.log(obj12);
// passing existing object
const obj123 = Object.create(user); // THIS WILLL CREATE A DEEP COPY OBJECT
obj123.college = "LPU";
console.log(obj123, user, " checlking whether shallow copy or deep copy");
console.log(" obj123 this object is created using Object.create", obj123);
// Object.assign THIS WILLL CREATE A shallow COPY OBJECT
const sourceObj = {
collegeName: "LPU",
onlinePortal: "Newton",
};
// source object
// object assign is creatin g shallow copy for targetobject
const targetObject = {};
const resultObjecty = Object.assign(targetObject, sourceObj); // this will return you the target object
console.log(resultObjecty);
console.log(targetObject === resultObjecty);
targetObject.cheker = true;
console.log(sourceObj, targetObject);