我想让这个语法成为可能:

var a = add(2)(3); //5

根据我读到的内容 http://dmitry.baranovskiy.com/post/31797647

我不知道如何使其成为可能。

有帮助吗?

解决方案

您需要添加到是一个函数,它的参数,并返回一个函数,其将所述参数来添加和本身的参数。

var add = function(x) {
    return function(y) { return x + y; };
}

其他提示

function add(x) {
    return function(y) {
        return x + y;
    };
}

嗯,JavaScript的的美

此语法是非常整齐以及

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
function add(x){
  return function(y){
    return x+y
  }
}

一流的功能关闭 做这份工作。

  

尝试,这将帮助你以两种方式添加(2)(3),并添加(2,3)

1)

 function add(a){ return function (b){return a+b;} }

    add(2)(3) // 5

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
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的一切,并且不需要终()在像一些其他的解决方案的功能的端。

console.log(add(1)(2)(3)(10));    // 16
console.log(add(10)(10));         // 20

这是关于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

它工作起来就像与无限添加链!!

一个魅力

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

这将同时处理

add(2,3) // 5

add(2)(3) // 5

这是一个ES6咖喱示例...

const add = (a, b) => (b || b === 0) ? a + b : (b) => a + b;

除了什么已经说过年代,这里与一般的钻营的解决方案(基于的 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))

这是广义的解决方案,将解决加载(2,3)()中,添加(2)(3)()或类似的附加的任意组合(2,1,3)(1)(1)(2, 3)(4)(4,1,1)()。请注意,几个安全检查都没有做,它可以进一步优化。

function add() {
	var total = 0;

	function sum(){
		if( arguments.length ){
			var arr = Array.prototype.slice.call(arguments).sort();
			total = total + arrayAdder(arr);
			return sum;
		}
		else{
			return total;
		}
	}

	if(arguments.length) {
		var arr1 = Array.prototype.slice.call(arguments).sort();
		var mytotal = arrayAdder(arr1);
		return sum(mytotal);
	}else{
		return sum();
	}

	function arrayAdder(arr){
		var x = 0;
		for (var i = 0; i < arr.length; i++) {
			x = x + arr[i];
		};
		return x;
	}
}
add(2,3)(1)(1)(1,2,3)();

封闭件的概念可以在这种情况下被使用。结果 功能“添加”返回另一个功能。返回的功能可以访问在父范围变量(在这种情况下,变量a)。

function add(a){

    return function(b){
        console.log(a + b);
    }

}


add(2)(3);

下面是一个链接到理解封闭 http://www.w3schools.com/js/js_function_closures的.asp

使用ES6传播...操作者和.reduce功能。与变异,你会得到链接语法,但最后一次通话()这里必须的,因为总是返回功能:

function add(...args) {
    if (!args.length) return 0;
    const result = args.reduce((accumulator, value) => accumulator + value, 0);
    const sum = (...innerArgs) => {
        if (innerArgs.length === 0) return result;
        return add(...args, ...innerArgs);    
    };
    return sum;
}




// it's just for fiddle output
document.getElementById('output').innerHTML = `
<br><br>add() === 0: ${add() === 0 ? 'true' : 'false, res=' + add()}
<br><br>add(1)(2)() === 3: ${add(1)(2)() === 3 ? 'true' : 'false, res=' + add(1)(2)()}
<br><br>add(1,2)() === 3: ${add(1,2)() === 3 ? 'true' : 'false, res=' + add(1,2)()}
<br><br>add(1)(1,1)() === 3: ${add(1)(1,1)() === 3 ? 'true' : 'false, res=' + add(1)(1,1)()}
<br><br>add(2,3)(1)(1)(1,2,3)() === 13: ${add(2,3)(1)(1)(1,2,3)() === 13 ? 'true' : 'false, res=' + add(2,3)(1)(1)(1,2,3)()}
`;
<div id='output'></div>

箭头功能无疑使其非常简单,以获得所需的结果:

const Sum = a => b => b ? Sum( a + b ) : a;

console.log(Sum(3)(4)(2)(5)()); //19

console.log(Sum(3)(4)(1)()); //8
function add(a, b){
 return a && b ? a+b : function(c){return a+c;}
}

console.log(add(2, 3));
console.log(add(2)(3));

函数add(){         VAR总和= 0;

    function add() {
        for (var i=0; i<arguments.length; i++) {
            sum += Number(arguments[i]);
        }
        return add;
    }
    add.valueOf = function valueOf(){
        return parseInt(sum);
    };
    return add.apply(null,arguments);
}

// ...

console.log(add() + 0);               // 0
console.log(add(1) + 0);/*                 // 1
console.log(add(1,2) + 0);               // 3
function A(a){
  return function B(b){
      return a+b;
  }
}

我发现对于这种类型的方法的一个很好的解释。这是被称为的闭包的语法

请参阅此链接 瓶盖的语法

const add = a => b => b ? add(a+b) : a;

console.log(add(1)(2)(3)());

或者(`${a} ${b}`)用于字符串。

只要我们可以写出这样的功能

    function sum(x){
      return function(y){
        return function(z){
          return x+y+z;
        }
      }
    }

    sum(2)(3)(4)//Output->9

<强>不变得复杂。

var add = (a)=>(b)=> b ? add(a+b) : a;
console.log(add(2)(3)()); // Output:5

将在最新的JavaScript(ES6)工作,这是一个递归函数。

下面我们使用封闭件的概念,其中称为主函数内的所有功能ITER参阅和UDPATE的 X 因为它们具有封闭了它。无论环多久去,到最后的功能,可以访问的 X

function iter(x){    
return function innfunc(y){
//if y is not undefined
if(y){
//closure over ancestor's x
x = y+x;
return innfunc;
}
else{
//closure over ancestor's x
return x;
    }
  }
}

ITER(2)(3)(4)()// 9    ITER(1)(3)(4)(5)()// 13

let multi = (a)=>{
	return (b)=>{
		return (c)=>{
			return a*b*c
		}
	}
}
multi (2)(3)(4) //24

let multi = (a)=> (b)=> (c)=> a*b*c;
multi (2)(3)(4) //24

function add () {
    var args = Array.prototype.slice.call(arguments);
 
    var fn = function () {
        var arg_fn = Array.prototype.slice.call(arguments);
        return add.apply(null, args.concat(arg_fn));
    }
 
    fn.valueOf = function () {
        return args.reduce(function(a, b) {
            return a + b;
        })
    }
 
    return fn;
}

console.log(add(1));
console.log(add(1)(2));
console.log(add(1)(2)(5));

http://www.cnblogs.com/coco1s/p/6509141。 HTML

let total = 0;
const add = (n) => {
if (n) {
    total += n;
    return add;
 }
}

add(1)(2)(3);
console.log(total);

这是错的?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top