Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make var a = add(2)(3); //5 work?

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

I've got no clue how to make it possible.

like image 686
rajakvk Avatar asked Feb 16 '10 12:02

rajakvk


People also ask

How do you do addition in JavaScript?

Add numbers in JavaScript by placing a plus sign between them. You can also use the following syntax to perform addition: var x+=y; The "+=" operator tells JavaScript to add the variable on the right side of the operator to the variable on the left.

What is the output of following code console log sum 2 )( 3 ))?

The code result = sum(2)(3) is effectively the same as f = sum(2); result = f(3) , without the intermediate variable.


1 Answers

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.

var add = function(x) {     return function(y) { return x + y; }; } 
like image 115
tvanfosson Avatar answered Oct 11 '22 13:10

tvanfosson