This article describes how to use the Array Function shift and pop in JS to create negligible parameters. This is a relatively advanced application, you can refer to the following two methods supported in JS Array: shift () and pop (), which respectively refer to deleting a value from the beginning and end of a data, and return to the deleted Value. Take a look at the example to understand:
The Code is as follows:
Var arr = ['s', 'O', 'F', 'I', 's', 'H'];
Arr. shift (); // returns 'S'
Arr; // currently ['O', 'F', 'I', 's', 'H']
Arr. pop () // return 'H'
Arr // currently ['O', 'F', 'I', 's']
In many JS frameworks, it can be very common that a method provides several parameters that you can pass. Some of these parameters can be ignored, and these negligible points may be the first, or the last one. The traditional method is to determine whether a parameter exists or the number of parameters determines the final value.
Here, we can use the arguments object of the function and shift and pop in the Array to implement flexible applications.
I. shift
How to implement a. bind () method to make the fn api as follows:
The Code is as follows:
// The fn scope is limited to the object
// Except the object, all bind method parameters will be passed to fn
Fn. bind (object, param1, param2, [, paramN]);
Look at an instance first. Of course, the call and apply applications may be more important in this example. However, we want to talk about the shift application:
The Code is as follows:
// ['. Bind'] (http://www.prototypejs.org/api/function/bind) method from Prototype. js
Function. prototype. bind = function (){
Var fn = this,
Args = Array. prototype. slice. call (arguments ),
Object = args. shift ();
Return function (){
Return fn. apply (object,
Args. concat (Array. prototype. slice. call (arguments )));
};
};
We can shift the arguments object (array-like object, which needs to be converted into a real array) to retrieve it. like this method, we mainly use them to separate objects as scopes, then skillfully pass the remaining parameter array to fn, that is, call the function that we want to limit to the object scope.
Ii. Use pop
We are trying to use seajs recently. Let's take an api of seajs for example:
The Code is as follows:
Define (id, dependencies, callback)
The api that defines a module, id and dependencies can be omitted. How can we implement this support? If you use the if clause to determine whether the request is true, you have to use a large number of if (arguments = 1) {...} elseif. Of course, this is also sometimes beneficial (?, Think about it ). Here, we may use pop to facilitate such support:
The Code is as follows:
Var define = function (){
// Retrieve this callback
Var args = []. slice. call (arguments)
Fn = args. pop ();
// Do other things
Fn. apply (null, args)
//...
},
Callback = function (){
Var args = arguments, I = 0, len = args. length;
If (len = 0) console. log ('only one callback ');
For (; I Console. log (args [I]);
}
}
// Check the execution results of the three of them.
Define (callback );
Define ('two parameters', callback );
Define ('Three parameters', 'Hello world', callback );
Two days ago, I used a reference when I used some JS skills with my colleagues. Although it is always called not to be too immersed in code, code, not just JavaScript, always gives us too much fun. Do not like it. Haha.