@@ -141,4 +141,138 @@ func2();
141141const func3 = ( a = 'Paul' , b = 'John' ) => {
142142console . log ( `${ a } & ${ b } = The Beatles` ) ;
143143} ;
144- func3 ( ) ;
144+ func3 ( ) ;
145+
146+ // Object Continues..
147+ // There are 2 ways to define objects
148+ // Constructor Syntax
149+ let obj2 = new Object ( ) ;
150+
151+ // Literal Syntax
152+ let obj3 = { } ;
153+
154+ // Object Literal and Properties
155+ obj2 = {
156+ name : 'Fred' ,
157+ age : 54 ,
158+ job : 'Artist' ,
159+ married : true ,
160+ }
161+
162+ // Accessing Object Properties
163+ // Dot Notation
164+ console . log ( `obj2.name = ${ obj2 . name } ` ) ;
165+ console . log ( `obj2.job = ${ obj2 . job } ` ) ;
166+
167+ // Square Brackets
168+ console . log ( `obj2['name'] = ${ obj2 [ 'name' ] } ` ) ;
169+ console . log ( `obj2['job'] = ${ obj2 [ 'job' ] } ` ) ;
170+
171+ // Adding a property in object
172+ obj2 . canDrive = true
173+ console . log ( obj2 ) ;
174+
175+ // Notice when we initialized obj2, we didn't quote string keys like we used to do in
176+ // Python dict. However, when you are passing multi word key, then in that case
177+ // you need to use quotes. For example,
178+ obj2 [ "pin code" ] = 205001 ;
179+ console . log ( obj2 ) ;
180+ // In above case, you can't use dot notation, either for declaring property or accessing it
181+ // For eg. - obj2.'pin code' = 205001 will throw error
182+
183+
184+ // Computed Properties
185+ // let fruit = prompt('Which fruit you want to buy?', null);
186+ let fruit = 'apple'
187+ let obj4 = {
188+ [ fruit ] :5 ,
189+ } ;
190+ console . log ( obj4 ) ;
191+
192+ // 'in' operator. Syntax - <'key' in object>
193+ console . log ( `'age' in obj2 = ${ 'age' in obj2 } ` ) ;
194+ console . log ( `'address' in obj2 = ${ 'address' in obj2 } ` ) ;
195+
196+ // for..in loop
197+ for ( let key in obj2 ) {
198+ console . log ( key ) ;
199+ }
200+
201+ // this keyword - It has different values depending on where it is used:
202+ /*
203+ - In a method, 'this' refers to the owner object
204+ - Alone, 'this' refers to the global object
205+ - In a function, 'this' refers to the global object
206+ - In a function, in strict mode, 'this' is undefined
207+ - In an event, 'this' refers to the element that received the event
208+ - Methods like call() and apply() can refer 'this' to any object
209+ */
210+
211+ // Loop in JS
212+ // for loop
213+
214+ for ( let i = 0 ; i < 3 ; i ++ ) {
215+ console . log ( i ) ;
216+ }
217+
218+ // break with label
219+
220+ loop1:
221+ for ( let i = 0 ; i < 5 ; i ++ ) {
222+ loop2:
223+ for ( let j = 0 ; j < i ; j ++ ) {
224+ if ( i === 4 ) {
225+ break loop1;
226+ }
227+ console . log ( j ) ;
228+ }
229+ }
230+
231+ // In similar manner, label can be used with continue
232+
233+ // while loop
234+ let temp5 = 121 ;
235+ let temp7 , temp6 ;
236+ temp6 , temp7 = 0 ;
237+ while ( temp5 != 0 ) {
238+ temp6 = temp5 % 10 ;
239+ temp5 = parseInt ( temp5 / 10 ) ;
240+ temp7 = ( temp7 * 10 ) + temp6 ;
241+ }
242+ console . log ( temp7 ) ;
243+
244+ //do while loop
245+ let temp8 = 1 ;
246+ do {
247+ console . log ( temp8 ) ;
248+ temp8 ++ ;
249+ } while ( temp8 < 5 ) ;
250+
251+ // arguments in JS
252+ // Arrow Function doesn't have arguments
253+ const func4 = function ( p1 , p2 ) {
254+ console . log ( `para1 = ${ p1 } , para2 = ${ p2 } , args = ${ arguments } ` ) ;
255+ console . log ( typeof ( arguments ) ) ;
256+ } ;
257+
258+ func4 ( 1 , 2 , 3 , 4 ) ;
259+
260+ // restArgs
261+
262+ const func5 = function ( para1 , para2 , ...restArgs ) {
263+ console . log ( `para1 = ${ para1 } , para2 = ${ para2 } , rest = ${ restArgs } ` ) ;
264+ console . log ( `typeof(restArgs) = ${ typeof ( restArgs ) } ` ) ;
265+ } ;
266+
267+ func5 ( 32 , 45 , 12 , 99 , 99 , 99 , 99 ) ;
268+
269+ /*
270+ 1 - Rest Parameters is a real array and methods like forEach and sort can be applied.
271+ Even though the arguments object has the length method, it is not a real array and using
272+ array methods like sort would only bring us misery and sorrow.
273+
274+ 2 -Rest Parameters contain only the arguments that have no corresponding parameter
275+ while arguments object contains all the arguments passed to the function.
276+ */
277+
278+
0 commit comments