File tree Expand file tree Collapse file tree 1 file changed +11
-17
lines changed Expand file tree Collapse file tree 1 file changed +11
-17
lines changed Original file line number Diff line number Diff line change 1
1
// call - runs instantly, arguments - list of items
2
+ // call - runs instantly, arguments - array of items
2
3
3
4
const john = {
4
5
name : "john" ,
5
6
age : 25 ,
6
- greet : function ( ) {
7
- console . log ( this ) ;
8
- console . log ( `Hi, my name is ${ this . name } and ${ this . age } ` ) ;
9
- } ,
10
7
} ;
11
8
12
9
const susan = {
13
10
name : "susan" ,
14
11
age : 21 ,
15
12
} ;
16
13
17
- function greet ( ) {
14
+ function greet ( city , country ) {
18
15
console . log ( this ) ;
19
- console . log ( `Hi, my name ${ this . name } and I am ${ this . age } years old` ) ;
16
+ console . log (
17
+ `Hi, my name ${ this . name } and I am ${ this . age } years old and I live in ${ city } , ${ country } `
18
+ ) ;
20
19
}
21
20
22
- // this will fail
23
- // susan.greet();
24
- // greet();
25
- const secondGreet = john . greet ;
26
- // secondGreet();
21
+ // greet.call(john, "san diego", "us");
22
+ // greet.call(susan, "san diego", "us");
23
+ // greet.call({ name: "peter", age: 30 }, "san diego", "us");
27
24
28
- greet . call ( john ) ;
29
- greet . call ( susan ) ;
30
- greet . call ( { name : "peter" , age : 30 } ) ;
31
-
32
- // Making "this" point to susan object
33
- john . greet . call ( susan ) ;
25
+ greet . apply ( john , [ "san diego" , "us" ] ) ;
26
+ greet . apply ( susan , [ "san diego" , "us" ] ) ;
27
+ greet . apply ( { name : "peter" , age : 30 } , [ "san diego" , "us" ] ) ;
You can’t perform that action at this time.
0 commit comments