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 11// call - runs instantly, arguments - list of items
2+ // call - runs instantly, arguments - array of items
23
34const john = {
45 name : "john" ,
56 age : 25 ,
6- greet : function ( ) {
7- console . log ( this ) ;
8- console . log ( `Hi, my name is ${ this . name } and ${ this . age } ` ) ;
9- } ,
107} ;
118
129const susan = {
1310 name : "susan" ,
1411 age : 21 ,
1512} ;
1613
17- function greet ( ) {
14+ function greet ( city , country ) {
1815 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+ ) ;
2019}
2120
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");
2724
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