1
+ const user = {
2
+ username : "vivek" ,
3
+ price : 3999 ,
4
+
5
+ welcomeMessage : function ( ) {
6
+ console . log ( `${ this . username } , welcome to the website` ) ;
7
+ // this keyword refers to the current context
8
+ console . log ( this ) ; // output the current object contents
9
+ }
10
+ }
11
+ // user.welcomeMessage()
12
+ // user.username = "sam" // sam will be the username for this context
13
+ // user.welcomeMessage()
14
+
15
+
16
+ // ******* Important ********
17
+ // console.log(this); // current context empty {} as we are in node env. and there is no context in the global
18
+ // but if you run this in browser, this (global object in browser) will be the window object
19
+
20
+ // const chai = function(){
21
+ // let username = "vivek"
22
+ // In normal functions, a this variable is created which references the
23
+ // objects that call them.
24
+ // console.log(this);
25
+ // console.log(this.username); // undefined, as this will only work in objects and not in functions
26
+ // }
27
+ // chai()
28
+
29
+ // arrow function
30
+ const chai = ( ) => {
31
+ let username = "vivek"
32
+ console . log ( this ) ; // Since chai is an arrow function, it doesn't create its own this variable
33
+ console . log ( this . username ) ; // undefined, as this will only work in objects and not in functions
34
+ }
35
+ chai ( )
36
+
37
+ // function print(){
38
+ // console.log(arguments);
39
+ // }
40
+ // print(1,false,"hello") // [Arguments] { '0': 1, '1': false, '2': 'hello' }
41
+
42
+ // const print = () =>{
43
+ // console.log(arguments); // there is no argument object in arrow function
44
+ // }
45
+ // print(1,false,"hello")
46
+
47
+ const addTwo = ( num1 , num2 ) => {
48
+ return num1 + num2 // return is mandatory if {} are used
49
+ }
50
+ console . log ( addTwo ( 3 , 6 ) )
51
+
52
+ // implicit return
53
+
54
+ // const addTwoOneLiner = (num1,num2) => (num1 + num2) // parenthesis are important for return if you want to return objects, see below
55
+ // const addTwoOneLiner = (num1,num2) => ({username : "vivek"})
56
+ console . log ( addTwoOneLiner ( 5 , 5 ) ) ;
0 commit comments