标签:兼容 掌握 console 固定 完成 简单的 war lang show
function add(a, b) { return a + b; } function curryingAdd(a) { return function(b) { return a + b; } } add(1, 2); // 3curryingAdd(1)(2); // 3
function printInfo(name, song) { console.log(name + ‘喜欢的歌曲是: ‘ + song); } printInfo(‘Tom‘, ‘七里香‘); printInfo(‘Jerry‘, ‘雅俗共赏‘); 对上面的函数进行柯里化之后,我们可以这样写: function curryingPrintInfo(name) { return function(song) { console.log(name + ‘喜欢的歌曲是: ‘ + song); } } var tomLike = curryingPrintInfo(‘Tom‘); tomLike(‘七里香‘); var jerryLike = curryingPrintInfo(‘Jerry‘); jerryLike(‘雅俗共赏‘);
function curryingHelper(fn) { var _args = Array.prototype.slice.call(arguments, 1); return function() { var _newArgs = Array.prototype.slice.call(arguments); var _totalArgs = _args.concat(_newArgs); return fn.apply(this, _totalArgs); } }
function showMsg(name, age, fruit) { console.log(‘My name is ‘ + name + ‘, I\‘m ‘ + age + ‘ years old, ‘ + ‘ and I like eat ‘ + fruit); } var curryingShowMsg1 = curryingHelper(showMsg, ‘dreamapple‘); curryingShowMsg1(22, ‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat apple var curryingShowMsg2 = curryingHelper(showMsg, ‘dreamapple‘, 20); curryingShowMsg2(‘watermelon‘); // My name is dreamapple, I‘m 20 years old, and I like eat watermelon
function betterCurryingHelper(fn, len) { var length = len || fn.length; return function () { var allArgsFulfilled = (arguments.length >= length); // 如果参数全部满足,就可以终止递归调用 if (allArgsFulfilled) { return fn.apply(this, arguments); } else { var argsNeedFulfilled = [fn].concat(Array.prototype.slice.call(arguments)); return betterCurryingHelper(curryingHelper.apply(this, argsNeedFulfilled), length - arguments.length); } }; }
var betterShowMsg = betterCurryingHelper(showMsg); betterShowMsg(‘dreamapple‘, 22, ‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applebetterShowMsg(‘dreamapple‘, 22)(‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applebetterShowMsg(‘dreamapple‘)(22, ‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applebetterShowMsg(‘dreamapple‘)(22)(‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat apple
var _ = {}; function crazyCurryingHelper(fn, length, args, holes) { length = length || fn.length; args = args || []; holes = holes || []; return function() { var _args = args.slice(), _holes = holes.slice(); // 存储接收到的args和holes的长度 var argLength = _args.length, holeLength = _holes.length; var allArgumentsSpecified = false; // 循环 var arg = null, i = 0, aLength = arguments.length; for(; i < aLength; i++) { arg = arguments[i]; if(arg === _ && holeLength) { // 循环holes的位置 holeLength--; _holes.push(_holes.shift()); } else if (arg === _) { // 存储hole就是_的位置 _holes.push(argLength + i); } else if (holeLength) { // 是否还有没有填补的hole // 在参数列表指定hole的地方插入当前参数 holeLength--; _args.splice(_holes.shift(), 0, arg); } else { // 不需要填补hole,直接添加到参数列表里面 _args.push(arg); } } // 判断是否所有的参数都已满足 allArgumentsSpecified = (_args.length >= length); if(allArgumentsSpecified) { return fn.apply(this, _args); } // 递归的进行柯里化 return crazyCurryingHelper.call(this, fn, length, _args, _holes); }; }
var crazyShowMsg = crazyCurryingHelper(showMsg); crazyShowMsg(_, 22)(‘dreamapple‘)(‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applecrazyShowMsg( _, 22, ‘apple‘)(‘dreamapple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applecrazyShowMsg( _, 22, _)(‘dreamapple‘, _, ‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applecrazyShowMsg( ‘dreamapple‘, _, _)(22)(‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat applecrazyShowMsg(‘dreamapple‘)(22)(‘apple‘); // My name is dreamapple, I‘m 22 years old, and I like eat apple
function hello(name) { console.log(‘Hello, ‘ + name); } setTimeout(hello(‘dreamapple‘), 3600); //立即执行,不会在3.6s后执行setTimeout(function() { hello(‘dreamapple‘); }, 3600); // 3.6s 后执行
setTimeout(hello.bind(this, ‘dreamapple‘), 3600); // 3.6s 之后执行函数
setTimeout(curryingHelper(hello, ‘dreamapple‘), 3600); // 其中curryingHelper是上面已经提及过的
function multiply(x) { var y = function(x) { return multiply(x * y); }; y.toString = y.valueOf = function() { return x; }; return y; } console.log(multiply(1)(2)(3) == 6); // trueconsole.log(multiply(1)(2)(3)(4)(5) == 120); // true
function add() { var args = Array.prototype.slice.call(arguments); var _that = this; return function() { var newArgs = Array.prototype.slice.call(arguments); var total = args.concat(newArgs); if(!arguments.length) { var result = 1; for(var i = 0; i < total.length; i++) { result *= total[i]; } return result; } else { return add.apply(_that, total); } } } add(1)(2)(3)(); // 6add(1, 2, 3)(); // 6
var addEvent = function (el, type, fn, capture) { if (window.addEventListener) { el.addEventListener(type, fn, capture); } else { el.attachEvent(‘on‘ + type, fn); } };
var addEvent = (function () { if (window.addEventListener) { return function (el, type, fn, capture) { el.addEventListener(type, fn, capture); } } else { return function (el, type, fn) { var IEtype = ‘on‘ + type; el.attachEvent(IEtype, fn); } } })();
function hello() { return function() { console.log(‘hello‘); if(!arguments.length) { console.log(‘from a anonymous function.‘); return arguments.callee; } } } hello()(1); // hello /* * hello * from a anonymous function. * hello * from a anonymous function. */hello()()();
function hello() { console.log(‘hello‘); console.log(hello.caller); } function callHello(fn) { return fn(); } callHello(hello); // hello [Function: callHello]
function f1(x, y) { return x + y; } function f2(x) { return x * x; } function func3(func1, func2) { return function() { return func2.call(this, func1.apply(this, arguments)); } } var f3 = func3(f1, f2); console.log(f3(2, 3)); // 25
// 一个将函数的arguments对象变成一个数组的方法function array(a, n) { return Array.prototype.slice.call(a, n || 0); } // 我们要运行的函数function showMsg(a, b, c){ return a * (b - c); } function partialLeft(f) { var args = arguments; return function() { var a = array(args, 1); a = a.concat(array(arguments)); console.log(a); // 打印实际传递到函数中的参数列表 return f.apply(this, a); } } function partialRight(f) { var args = arguments; return function() { var a = array(arguments); a = a.concat(array(args, 1)); console.log(a); // 打印实际传递到函数中的参数列表 return f.apply(this, a); } } function partial(f) { var args = arguments; return function() { var a = array(args, 1); var i = 0; j = 0; for(; i < a.length; i++) { if(a[i] === undefined) { a[i] = arguments[j++]; } } a = a.concat(array(arguments, j)); console.log(a); // 打印实际传递到函数中的参数列表 return f.apply(this, a); } } partialLeft(showMsg, 1)(2, 3); // 实际参数列表: [1, 2, 3] 所以结果是 1 * (2 - 3) = -1partialRight(showMsg, 1)(2, 3); // 实际参数列表: [2, 3, 1] 所以结果是 2 * (3 - 1) = 4partial(showMsg, undefined, 1)(2, 3); // 实际参数列表: [2, 1, 3] 所以结果是 2 * (1 - 3) = -4
转载至:https://www.jianshu.com/p/f02148c64bed?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation(鸣谢)
标签:兼容 掌握 console 固定 完成 简单的 war lang show
原文地址:https://www.cnblogs.com/bgwhite/p/9407965.html