Each function contains two non-inherited methods: Apply () and call (). The purpose of both methods is to invoke the function in a specific scope, and the specific scope is the object specified by the This parameter.
The really powerful place for apply () and call () is the ability to extend the scope in which functions are run. Let's look at an example:
Window.color = "Red" ; var o = {color: "Blue" function Saycolor () {alert ( this .color);} Saycolor (); // red Saycolor.call (this ); // red saycolor.call (window); // red saycolor.call (o); // blue
Saycolor () is defined as a global function, and when it is called in the global scope, it does display "red"-because the evaluation of This.color is converted to window.color evaluation. Saycolor.call (this) and saycolor.call (window) are two ways to explicitly call a function in the global scope, and the results will of course show "Red".
However, when you run Saycolor.call (o), the execution environment of the function is different, because the this object in the function body points to O, and the result shows "blue".
The biggest benefit of using call () or apply () to extend the scope is that the object does not need to have any coupling with the method.
The Apply () and call () methods of the Function object