This article according to some of the big guys in the reply finishing, if there is deeper insight, welcome to join the discussion ~ ~
In JS oop, this is often defined as:
function= { ' fish ', function() { Console.log ( the. food); } } var New //I Love fish
Call and apply are used to dynamically change this, and when an object does not have a method, but other methods are available, you can use call or apply to manipulate the other object's methods:
function= { ' fish ', function() { Console.log ( the. food); } } var New //I love fishvar whitedog = {food: ' bone '//I Love Bone
The difference between calls and apply is that call needs to pass parameters in order, and apply is to pass the parameters in the array.
Therefore, the number of parameters of a function in JS is not fixed, you can use apply, the parameter push into the array to pass, you can also use the arguments object to traverse to get all parameters, when the parameters are clearly known, the number can be used when the call.
As an example:
The DOM node selected through document.getElementsByTagName is an array-like object, but it cannot be applied using push, pop, etc. under the array, we can:
var domnodes = Array.prototype.slice.call (document.getElementsByTagName (' * '));
This way the domnodes can use all the methods under the array.
Call and apply