JS Array supports two methods, shift() and pop(), which respectively refer to deleting a value from the front and end of a data and returning the deleted value. Just look at an example to understand:
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 it is ['o','f','i','s']
It is very common in many JS frameworks, A method allows you to pass several parameters, and some of these parameters can be ignored. These ignored points may be the first or the last. The traditional way of writing is to determine whether the parameter exists, or the number of parameters to determine the final value.
Here, we can use the arguments object of the function, as well as shift and pop in Array to achieve flexible applications.
1. How to implement a .bind() method using shift
, so that the fn api is as follows:
// The scope of fn is limited to object
// Except for object, all the parameters of the bind method will be passed to fn
fn.bind(object, param1, param2, [, paramN]);
Let’s look at an example first. Of course, what may be more important in this example is the application of call and apply. However, what we want to talk about is the application of shift:
//[`.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 use the arguments object (array-like object, which needs to be converted into a real array) to take out by shift. Like this method, we mainly use them to separate the object as the scope, and then cleverly pass the remaining parameter array to fn, that is, call the function we want to limit to the object scope.
2. Use pop
I am trying out seajs recently. Let’s take one of its APIs as an example:
define(id, dependencies, callback)
This defines the api of a module, id and dependencies can be omitted. Here, how to implement this support? If you use if to judge, you really have to use if (arguments === 1) {...} elseif ... a lot. Of course, this sometimes has advantages (?, think about it). Here, we may use pop to facilitate the implementation of such support:
var define = function(){
// Take out this callback
var args = [].slice.call(arguments)
fn = args.pop();
// Do something Other interesting 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 ]);
}
}
// Take a look at the execution results of the three of them
define(callback);
define('has two parameters', callback);
define('has three parameters', 'hello world ', callback);
A reference I made two days ago when my colleagues and I were learning some techniques in JS. Although I always tell myself not to be too immersed in code, code, not just JS, always gives us too much fun. How not to like. Ha ha.