Call () and apply () are all the same-by changing the direction of this within the function body, the purpose of the method of borrowing objects
Just give me a chestnut:
function Cat () { This.food = ' fish '; this.say= function () { console.log (' I love ' + This.food) }} //kitten constructor var Keke = new Cat (); A kitten, created by the kitten constructor var Diandian = {food: ' bone '}; Another puppy.
Kitty and puppy go to the restaurant for dinner
Keke.say (); I Love Fish
The puppy has no say method, but he also wants to say his favorite food, so Keke.say.call (Diandian); I Love Bone The way to borrow kittens. The two people ate their favorite food together happily.
The difference between the two methods is that the arguments, call () arguments passed to the function must be enumerated, and apply () passes the parameter array.
Call () and apply () usage