Advanced usage in JavaScript functions

Source: Internet
Author: User

First introduce the high-order function in JS, the so-called high-order function is that the parameter in a function is a function or return is a function, called the higher order function.

JS has improved the higher-order function, the use is very good, of course, we can also implement, I introduce several es5/es6 new array functions.

The first is Foreach,foreach it accepts two parameters, the first function, the second passes a this reference object (can not be passed), the function supports 3 arguments the first represents the current value of the traversal, the second is an index, and the third represents the current object.

[1,2,4,5].foreach (function (item,index) {
Console.log (Item,index);
})


Simulation of the implementation of foreach

Array.prototype.for = function (fn) {
for (Var i=0;i<this.length;i++) {
FN (this[i],i);
}
};
[2,3,4,5,6].for (function (item,index) {
Console.log (Item,index);
})

Map returns a new array after processing


var arr = [1,2,3,4,5];
var a = Arr.map (function (item,index) {
if (index>0) {
return item*index;
}
return item;
})

Console.log (a); [1, 2, 6, 12, 20]
Console.log (arr); [1, 2, 3, 4, 5]

Analog Implementation Map


var arr = [1,2,3,4,5];
ARRAY.PROTOTYPE.M = function (fn) {
var arr = [];
for (Var i=0;i<this.length;i++) {
Arr.push (FN (this[i],i,this));
}
return arr;
};
Console.log (ARR.M (function (item,index,thisvalue) {
Console.log (Thisvalue); [1, 2, 3, 4, 5]
if (item>3) {
return item;
}
return item-1;
}))//[0, 1, 2, 4, 5]

  

The some method is used to detect whether an element in an array satisfies a specified condition, and if an element satisfies a condition, the expression returns True, the remaining elements are no longer instrumented, and false if there are no elements that satisfy the condition.

Array.prototype.so = function (fn) {
for (Var i=0;i<this.length;i++) {
if (FN (this[i],i,this)) {
return true;
};
}
return false;
};

var arr = [1,2,3,4,5,6];

Console.log (function (item) {arr.so
Return item>7;
}))

Filter returns the value that satisfies the condition, which is a new array.

ARRAY.PROTOTYPE.F = function (fn) {
var arr = [];
for (Var i=0;i<this.length;i++) {
FN (this[i],i,this) &&arr.push (This[i]);
}
return arr;
};
var arr = [1,2,3,4,5,6];
Console.log (function (item) {ARR.F
Return item<2;
}))

Every detects whether all elements of an array meet the specified criteria, and if an element is detected in the array that is not satisfied, the entire expression returns false, and the remaining elements are no longer instrumented.


Array.prototype.ev = function (fn) {
for (Var i=0;i<this.length;i++) {
if (!FN (this[i],i,this)) {
return false;
};
}
return true;
};

var arr = [n/a];

Console.log (function (item) {Arr.ev
Return item>1;
}))

function currying

The simple thing is that we execute a function in some cases, we don't need it to return some value directly to us, but it returns us again when we need it, this is the function of the curry, the following is written by a certain Daniel.

A currying function will first accept some parameters, after accepting these parameters, the function will not be evaluated immediately, but continue to return another function, just passed the parameter in the function formed in the closure of the save. Until the function is truly evaluated, all previously passed parameters are evaluated once.

Not fully curry version of

var fn = (function () {
var num = 0;
return function () {
if (arguments.length===0) {
return num;
}else{
for (Var i=0;i<arguments.length;i+=1) {
Num+=arguments[i];
}
}
};
}())

FN (2);
FN (2,2,2);
FN (2);
Console.log (FN ()); 10

Curry version of

var currying = function (fn) {
var arrnum = [];
return function () {
if (arguments.length===0) {
Return fn.apply (This,arrnum);
}else{
[].push.apply (Arrnum,arguments);
return Arguments.callee;
}
};
}

var count = (function () {
var num = 0;
return function () {
for (Var i=0;i<arguments.length;i+=1) {
Num+=arguments[i];
}
return num;
};
}());

var s = currying (count);
S (3);
S (3,3,3);


Console.log (S ());

function Inverse currying uncurrying
Create a function with a wider range of applications. Extend to more objects a method that would otherwise apply only to a particular object.

Simple version

var obj = {
"Length": 3,
"0": 1,
"1": 2,
"2": 3
};

Array.prototype.push.call (obj,3);
Console.log (obj);

function push () {
var a = [].shift.call (arguments);
Array.prototype.push.apply (a,arguments);
};
Push (obj,3);
Push (obj,30);
Push (obj, ' js ');
Console.log (obj);


You can do the same.

var obj = {
"Length": 3,
"0": 1,
"1": 2,
"2": 3
};

Array.prototype.push.call (obj,3);
Console.log (obj);

Function.prototype.uncurrying = function () {
var _this = this; Array.prototype.push
return function () {
Deletes the first, and returns the Obj
var obj = [].shift.call (arguments);
Obj calls the Array.prototype.push method, passing arguments, noting that at this point the arguments already does not contain the parameter obj.
Return _this.apply (obj,arguments);
};
};

var push = Array.prototype.push.uncurrying ();

Console.log (push (obj, ' 666 '));
Console.log (obj)
Push (obj, ' 777 ');
Console.log (obj)

Advanced usage in JavaScript functions

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.