-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
214 lines (148 loc) · 4.28 KB
/
Copy pathobject.js
File metadata and controls
214 lines (148 loc) · 4.28 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// // Object-oriented- programming
// in this we try to solve problems using objects rather than orthodox style
// const circle= {
// radius : 1,
// location: {
// x:1,
// y:1
// },
// isvisible: true,
//draw(){} is also a wayy
// ~// draw : function(){
// console.log('draw');
// }
// };
// circle.draw(); //method
// function inside object is called method
//FACTORY FUNCTION'
// it is used when we wanna create more
// than one circles(example) but some properties will remain
// same so its very useful at these kind of places
// camel notation is used in factory function
// function createCricle(radius){
// return {
// radius, //equivalent to ->radius :radius same name wale me chlta h aisa
// draw()
// {
// console.log('draw');
// }
// };
// }
// const circle1 = createCricle(1);
// console.log(circle1);
// const circle2 = createCricle(2);
// console.log(circle2);
// CONSTRUCTOR FUNCTION -> PASCAL NOTATION IS USED
// function Circle(radius){
// this.radius=radius;
// this.draw =function(){
// console.log('draw');
// }
// // return this; it will happen under the hood
// }
// const circle = *new* Circle(1); * aise hi h ////new keyword is impp
// console.log(circle);
// // const x={}; // this is happening because of new keyword
//1 it makes empty js object under the hood
//2 it will set 'this' to point to this empty object
// 3 it will return the object from the function
//DYNAMIC NATURE
// const circle={
// radius:1
// };
// circle.color='yelllow';
// circle.draw = function(){};
// delete circle.color;
// console.log(circle);
//CONSTRUCTOR PROPERTY
// every obj in js has constructor property
// in js functions are objects
//primitives(value types)->string boolean, number etc are copied by their value
//ref types->obj fun array\\objects are copied by their reference
// let num= 10;
// function increment(num){
// num++; //completely independent
// }
// increment(num);
// console.log(num);
// let num= {value: 10};
// function increment(num){
// num.value++;
// }
// increment(num);
// console.log(num.value);
//enumerating properties of object
// const circle={
// radius:1,
// draw(){
// console.log('draw');
// }
// };
//ways of enumerating
//simplest way is this
// for(let key in circle)
// console.log(key, circle[key]);
//way2-> keys
// for(let key of circle)
// console.log(key); // get error cz its not iterable
// for(let key of Object.keys(circle)) //neeche iska btaya h
// console.log(key);
// ye ho rha h andrr
// function Object(){}
// const x= { value :1};
// const x = new Object();
//way3->entries
// for(let entry of Object.entries(circle))
// console.log(entry);
//to check if we have prtcular prprty or method
// if('radius' in circle) console.log('yes');
///////CLONING OF AN OBJECT/////
// const circle={
// radius:1,
// draw(){
// console.log('draw');
// }
// };
//way 1
// const another ={};
// for(let key in circle){
// another[key]=circle[key];
// }
// console.log(another);
//way 2
// using Object.assign(can be empty operator, jisse copy krna h wo);
//can be used to combine multiple methods
// const another = Object.assign({},circle);
// const another = Object.assign({color:'yell'},circle);
// console.log(another);
//way 3 using spread operator
// const another={ ...circle};
// console.log(another);
//in js we dont hve to worry about garbage collection unlike c++
//strings
// const message ="hi";
// message.length
// console.log(typeof(another));
// primitive hoti h na string toh.. fir kaise properties show ho rhi h
// toh js automatically string ko bhi object jaise properties show krta h
//we can create object of string anyway
// const another = new Object("hi");
// console.log(typeof(another)); // it will be object
// String.-> TEMPLATE LITERALS
// VERY USEFUL
// `` BACKTICKS IS USED instead of '' single or double quote
// const msg=`hello 'fuckcers' `;
// console.log(msg);
// const msg=
// ` hi john,
// how are ya??
// `
// console.log(msg);
// using template we can eliminate the \n and jo bhi faltu chize use krni pdti h quotes use krke
// ${} use krke we can dynamically enter the *naame or anything//depreceated
// const naame=`ved`;
// const msg=
// ` hi ${naame} ${2+3},
// how are ya??
// `
// console.log(msg);