Introduction to the use of shift and pop techniques in JavaScript array arrays

Source: Internet
Author: User
Tags bind javascript array

JS Array supports two methods, 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:

The code is as follows Copy Code

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:

The

//FN scope is defined under Object
//Except object, and the parameters of all bind methods are passed to the FN
Fn.bind (object, param1, param2, [, Paramn]); see one instance 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:

  code is as follows copy code
//from Prototype.js ['. bind '] (http://www.prototypejs.org/api/function/bind) method
Function.prototype.bind = function () {
  var fn = this,
      args = Array.prototype.slice.call (arguments),
 & nbsp;    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:

Define (ID, dependencies, callback) This definition of a module of api,id and dependencies 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:

The code is as follows Copy Code

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.

  code is as follows copy code
define ( callback);
define (' two parameters ', callback);
define (' Three parameters ', ' Hello World ', callback);

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.