Let you understand call, apply, bind applications and differences

call, apply, bind and use the difference

// a cat called black, black fish will 
const CAT = { 
    name: 'black' , 
    eatFish (... args) { 
        the console.log ( 'the this point =>', the this ); 
        the console.log ( 'args ...' , args); 
        the console.log ( the this .name + 'fish' ); 
    } 
} 
// have only dog ovo ovo eat bones 
const Dog = { 
    name: 'ovo' , 
    eatBone (... args) { 
        the console.log ( 'the this point =>', the this ); 
        the console.log ( 'args ...' , args); 
        Console.log(this.name + 'a bone,' ); 
    }, 
} 

the console.log ( 'Call ============= =================== ============ ' );
 // One day ovo want to eat fish, but it does not know how to eat. How to do? Black said that I eat when you eat feed 
cat.eatFish.call (dog, 'bark Wang', 'Call' )
 // ovo order to express gratitude, decided to eat next time also fed black bones to eat 
dog.eatBone. call (cat, 'meow meow', 'Call' ) 

the console.log ( '=================== Apply =========== ============== ' ); 
cat.eatFish.apply (Dog, [ ' bark Wang ',' Apply ' ]) 
dog.eatBone.apply (CAT, [ ' meow meow ' , 'Apply' ]) 

the console.log ( '=================== the bind =================== ====== ' );
// One day they think that every time when eating and then fed too much trouble. Simply direct teach each other how to eat
const test1 = cat.eatFish.bind(dog, '汪汪汪', 'bind')
const test2 = dog.eatBone.bind(cat, '喵喵喵', 'bind')
test1()
test2()

See article

Guess you like

Origin www.cnblogs.com/xiaozhumaopao/p/11116544.html