Apply () method definition
The Apply () method of a function works the same as the call method, except that the parameters are received in a different way.
The Apply () method receives two parameters, one is an object, and the other is an array of arguments.
Apply () function
1, to extend the scope of the function
Example:
var color= ' red ';
var o={color: ' Blue '};
function Saycolor () {
console.log (this.color);
}
Saycolor ()//"Red"
Here the function is dynamically bound to the object o by the Apply () method, when this points to the O object and gets the result "blue".
2, the object does not need to have any coupling relationship with the method
Here's a couple of examples to see how to solve this coupling with apply.
var color= ' red ';
var o={color: ' Blue '};
function Saycolor () {
console.log (this.color);
}
O.saycolor=saycolor;
Here we first put the function in object o, where the objects and methods are tightly coupled, and the method calls must go through object o.
Not as flexible as using the Apply () and call () methods.
Refactor the code above to get the code in the previous precedent.
var color= ' red ';
var o={color: ' Blue '};
function Saycolor () {
console.log (this.color);
}
Saycolor ()//"Red"
The object is not bound to any method, but is dynamically bound using the Apply or call method of the function when it is needed.
Objects and methods are not coupled together. You can also use the bind () method provided by ES5 to complete
3. Implement variable parameter function
The following function that calculates the average of any number of digits
Average (,,);
Average ();
Average (,,,,,,,,);
The average function is an example of a variable parameter or variable element function (the number of arguments the function is expected to refer to).
Of course, this function can also be written as a receiving array form.
Averageofarray ([,,]);
Averageofarray ([]);
Averageofarray ([,,,,,,,,]);
Functions that use variable parameters are more concise and elegant. Variable-parameter functions have convenient syntax, at least to give callers a clear idea of how many arguments are provided in advance.
If I had such an array
How do you use the average function to calculate averages?
1. variable parameter function version.
This can be used in conjunction with the Apply () method, because the function does not reference the this variable, so the first argument we pass in a null. The code is as follows:
var scores=getallscores ();
2. Direct parameters in the form of arrays
Here you can pass in the array parameters directly.
var scores=getallscores ();
The above two forms, the individual feels is OK, but the second kind is simpler. Knowing one way to do this is easy to deal with when someone else writes a function, and you don't need to refactor the code. The advantage is more.
4, the realization of variable parameter method of transfer value
Example: The Buffer object contains a append method of a variable parameter that adds elements to the state array inside the function.
var buffer={
state:[],
append:function () {for
(var i=,n=arguments.length;i<n;i++) {
This.state.push (Arguments[i]);
}
The Append method can then accept any number of parameters.
Buffer.append (' Hello, ');
Buffer.append (' Firtname ', ', ', ' lastName ', '! ');
Forms such as
With the This parameter of the Apply method, we can specify a computable array to invoke the Append method
Note: The buffer is important here, and if different objects are passed, the Append method will attempt to modify the state property of the Error object .
Tips
• Use the Apply method to specify a computable parameter array to invoke a function of a variable parameter
• Use the first parameter of the Apply method to provide a recipient for the variable parameter method
Appendix I
Average function
function average () {
var args=[].slice.call (arguments);
var sum=args.reduce (function (prev,cur) {return
prev+cur;
});
Return parseint (Sum/args.length,);
Averageofarray function
function Averageofarray (arr) {
var sum=arr.reduce (function (prev,cur) {return
prev+cur;
});
Return parseint (Sum/arr.length,);
ES5 bind () method
This method creates an instance of a function whose this value is bound to the value passed to the bind () function.
For example
var color= ' red ';
var o={color: ' Blue '};
function Saycolor () {
console.log (this.color);
}
var osaycolor=saycolor.bind (o);
Compatible with the lower version, refer to the following version:
if (! Function.prototype.bind) {
Function.prototype.bind = Function (othis) {
if (typeof this!== ' function ') {
// Closest thing possible to the ECMAScript
//internal iscallable function
throw new TypeError (' Function.prototype . Bind-what is trying to be bound isn't callable ');
var Aargs = [].slice.call (arguments,),
ftobind = this,
Fnop = function () {},
Fbound = function () {
Retu RN ftobind.apply (This instanceof fnop this:othis, Aargs.concat (Array.prototype.slice.call
(arguments)));
if (this.prototype) {
//Function.prototype doesn ' t have a prototype property
Fnop.prototype = This.prototype;
}
Fbound.prototype = new Fnop ();
return fbound;
};