Examples of using the array function shift and pop to create negligible parameters in JS _javascript tips

Source: Internet
Author: User

Two methods are supported in the JS Array, Shift () and pop (), which is to delete a value from the front and back of a data, and to delete the value. Look at an example and you'll see:

Copy Code code as follows:

var arr = [' s ', ' o ', ' f ', ' I ', ' s ', ' H '];

Arr.shift (); Return to ' s '
Arr is currently [' O ', ' f ', ' I ', ' s ', ' H ']
Arr.pop ()//Return ' H '
ARR//is currently [' O ', ' f ', ' I ', ' s ']


In many JS frameworks it is very common that a method provides you with several parameters, some of which can be ignored, and the points that can be ignored may be the first or the last. The traditional writing is to determine whether the parameters exist, or the number of parameters to determine the final value.

Here, we can use the arguments object of the function and the shift and pop in the Array to make the application flexible.

First, use shift
How to implement A. bind () method so that the FN API is as follows:

Copy Code code as follows:

The scope of FN is limited to the object
All the parameters of the Bind method, except object, are passed to the FN
Fn.bind (object, param1, param2, [, Paramn]);

Look at an example first. Of course, this example may be more important than the application of call and apply. However, what we want to say is the shift application:
Copy Code code 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 take shift to the arguments object (Array-like object, which needs to be converted to a real array), like this method, which mainly uses them to separate objects as scopes, and then subtly passes the remaining argument arrays to FN, That calls the function that we want to qualify to the object scope.

Second, the use of pop
Recently on trial Seajs, let's take one of its APIs:

Copy Code code as follows:

Define (ID, dependencies, callback)

The Api,id and dependencies that define a module can be omitted. Here, how to achieve this support? If you use if to judge, you really get if (arguments = = 1) {...} ElseIf ...} A whole bunch of them. Of course, this is sometimes good (?, think). Here, we may use pop to facilitate this support:
Copy Code code as follows:

var define = function () {
Take out this callback.
var args = [].slice.call (arguments)
fn = Args.pop ();
Do some other God-horse thing.
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]);
}
}

Look at the results of their three execution.
Define (callback);
Define (' Two parameters ', callback);
Define (' Three parameters ', ' Hello World ', callback);


The first two days and colleagues in addition to some of the skills in JS refers to a thing. Although I always call myself not too immersed in the code, but the code, not only JS, always give us too much fun. How do not like. Ha ha.

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.