Javascript 我怎样才能让 var a = add(2)(3); //5 工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2272902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make var a = add(2)(3); //5 work?
提问by rajakvk
I want to make this syntax possible:
我想让这个语法成为可能:
var a = add(2)(3); //5
based on what I read at http://dmitry.baranovskiy.com/post/31797647
基于我在http://dmitry.baranovskiy.com/post/31797647上阅读的内容
I've got no clue how to make it possible.
我不知道如何使它成为可能。
回答by tvanfosson
You need add to be a function that takes an argument and returns a function that takes an argument that adds the argument to add and itself.
您需要将 add 作为一个函数,它接受一个参数并返回一个接受一个参数的函数,该参数将参数添加到 add 和它本身。
var add = function(x) {
return function(y) { return x + y; };
}
回答by Oscar Kilhed
function add(x) {
return function(y) {
return x + y;
};
}
Ah, the beauty of JavaScript
啊,JavaScript 之美
This syntax is pretty neat as well
这个语法也很简洁
function add(x) {
return function(y) {
if (typeof y !== 'undefined') {
x = x + y;
return arguments.callee;
} else {
return x;
}
};
}
add(1)(2)(3)(); //6
add(1)(1)(1)(1)(1)(1)(); //6
回答by NVI
function add(x){
return function(y){
return x+y
}
}
First-class functionsand closuresdo the job.
回答by vasudev
try this will help you in two ways add(2)(3) and add(2,3)
试试这会以两种方式帮助你 add(2)(3) 和 add(2,3)
1.)
1.)
function add(a){ return function (b){return a+b;} }
add(2)(3) // 5
2.)
2.)
function add(a,b){
var ddd = function (b){return a+b;};
if(typeof b =='undefined'){
return ddd;
}else{
return ddd(b);
}
}
add(2)(3) // 5
add(2,3) // 5
回答by Ben Hernandez
function add(n) {
sum = n;
const proxy = new Proxy(function a () {}, {
get (obj, key) {
return () => sum;
},
apply (receiver, ...args) {
sum += args[1][0];
return proxy;
},
});
return proxy
}
Works for everything and doesn't need the final () at the end of the function like some other solutions.
适用于所有情况,并且不需要像其他一些解决方案那样在函数末尾使用 final ()。
console.log(add(1)(2)(3)(10)); // 16
console.log(add(10)(10)); // 20
回答by satyrsynth
ES6 syntax makes this nice and simple:
ES6 语法让这一切变得美好而简单:
const add = (a, b) => a + b;
console.log(add(2, 5));
// output: 7
const add2 = a => b => a + b;
console.log(add2(2)(5));
// output: 7
回答by hien
It's about JS curring and a little strict with valueOf:
这是关于 JS curring 并且有点严格valueOf:
function add(n){
var addNext = function(x) {
return add(n + x);
};
addNext.valueOf = function() {
return n;
};
return addNext;
}
console.log(add(1)(2)(3)==6);//true
console.log(add(1)(2)(3)(4)==10);//true
It works like a charm with an unlimited adding chain!!
它就像一个无限添加链的魅力!!
回答by user187291
in addition to what's already said, here's a solution with generic currying (based on http://github.com/sstephenson/prototype/blob/master/src/lang/function.js#L180)
除了已经说过的内容之外,这里还有一个通用柯里化的解决方案(基于http://github.com/sstephenson/prototype/blob/master/src/lang/function.js#L180)
Function.prototype.curry = function() {
if (!arguments.length) return this;
var __method = this, args = [].slice.call(arguments, 0);
return function() {
return __method.apply(this, [].concat(
[].slice.call(args, 0),
[].slice.call(arguments, 0)));
}
}
add = function(x) {
return (function (x, y) { return x + y }).curry(x)
}
console.log(add(2)(3))
回答by Amir Danish
Arrow functions undoubtedly make it pretty simple to get the required result:
箭头函数无疑使获得所需结果变得非常简单:
const Sum = a => b => b ? Sum( a + b ) : a;
console.log(Sum(3)(4)(2)(5)()); //14
console.log(Sum(3)(4)(1)()); //8
回答by Joshua Michael Waggoner
This will handle both
这将同时处理
add(2,3) // 5
or
或者
add(2)(3) // 5
This is an ES6 curry example...
这是一个 ES6 咖喱示例...
const add = (a, b) => (b || b === 0) ? a + b : (b) => a + b;

