[索引]
[1.參數(shù)轉(zhuǎn)數(shù)組]
[2.改變方法的this]
[3.函數(shù)柯里化]
1.參數(shù)轉(zhuǎn)數(shù)組
function log(c){
console.log([].slice.call(arguments));
//console.log(Array.prototype.slice.call(arguments));
}
log('a','c');
輸出
['a','c']
2.改變方法的this
function log(){
function _log(c){
console.log(c)
return this;
};
return _log.bind(_log)([].slice.call(arguments)[0]);
// return _log.apply(_log,[].slice.call(arguments).slice(0));
// return _log.call(_log,[].slice.call(arguments)[0]);
}
log('1')('2');
輸出
1
2
3.函數(shù)柯里化
function add(x,y,z){
return x+y+z;
}
function curryFunc(func){
var args = [];
var len = func.length;;
return function _curryFunc(){
[].push.apply(args, [].slice.apply(arguments));
if(args.length===len){
return func.apply(this,args);
}
return _curryFunc;
}
}
console.log(curryFunc(add)(2,2)(2));
console.log(curryFunc(add)(2)(2)(2));
console.log(curryFunc(add)(2)(2,2));
輸出
6
6
6
柯里化(Currying)是把接受多個(gè)參數(shù)的函數(shù)變換成接受一個(gè)單一參數(shù)(最初函數(shù)的第一個(gè)參數(shù))的函數(shù)疑故,并且返回接受余下的參數(shù)且返回結(jié)果的新函數(shù)的技術(shù)
待續(xù)....