深入浅出Node.js - 异步编程(4.1-)

1.函数式编程

  • 高阶函数:将函数作为参数或返回值的函数
  • 偏函数:参数和变量已经预置的函数的函数,如:
1
2
3
4
5
6
7
8
9
var isType = function(type) {
return function(obj) {
var toString = Object.prototype.toString;
return toString.call(obj) == '[object' + type + ']';
}
}
//创建isString()、isFunction()
var isString = isType('String');
var isFunction = isType('Function');

Underscoreafter()是偏函数的应用:

1
2
3
4
5
6
7
8
9
10
_.after = function(times, func) {
if (times <= 0) {
return func();
}
return function() {
if (--times < 1) {
return func.apply(this.argments);
};
};
};