Example of using the Array Function shift and pop in JS to create negligible Parameters

Source: Internet
Author: User

In JS Array, two methods are supported: shift () and pop (), which are used to delete a value from the beginning and end of a data and return to the deleted Value. Take a look at the example to understand:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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 <len; 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.