javascript multiple parentheses function call [duplicate] - javascript

This question already has answers here:
What is 'Currying'?
(23 answers)
Closed last year.
I'm trying to solve some javascript puzzle. I have some function (for example a function which calculates summa).
But there is a weird calling of that function.
example:
sum(5)
or sum(5,6) - should be 11
or sum(5)(6) - should be 11
or sum(1)(2)(3) - should be 6
Have never seen such function calling sum(1)(2)(3)(4)....
Can you please explain or maybe put into documentation where this stuff is explained?
Thanks a lot.

If the sum function returns a value that it can reuse, you can chain the numbers on and on.
If you can share the function it can be easier to explain

Related

Functions Considered as Objects in JavaScript [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 2 years ago.
I am new to JavaScript (and StackOverflow) and am hoping to get some help on a problem that has bothered me for some time. I understand that functions are considered objects in JS. A function is different from an object in the sense that it can execute code.
This may be a very simple and straightforward question to some of you veteran folks. I don't understand how the code works below. Essentially there is a function count() that returns an anonymous counter function. keepCount points to the anonymous function (an object, of course) that count() returns.
function count() {
var num = 0;
return function(correct) {
if (correct)
num++;
return num;
}
}
var keepCount = count();
keepCount(true); // num is 1
keepCount(true); // num is 2
keepCount(true); // 3
keepCount(true); // 4
console.log(keepCount(true)); // Call it again and print. It is 5.
My question: What is causing the result of num to be 'saved' or recorded with each function call? Isn't num a variable local to count() — the outer function? num does not appear to be a property of the anonymous function. I suspect the answer has something to do with the fact that functions are considered objects in JS, and that a local variable can be continuously updated in the variable object of count(). A new variable object is not produced for count() with each call of the anonymous function.
I would also appreciate comments on the formatting of this question and all. I want to be sure that I am following StackOverflow guidelines properly. I also hope that the details of the question make sense. Please let me know if anyone requires clarification.

Javascript: What is function with two parenthesis brackets ()()? [duplicate]

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
What is 'Currying'?
(23 answers)
Closed 5 years ago.
I recall somewhere seeing a function with two parenthesis brackets ()() like:
function add_numbers(number1)(number2)
What do you call such a function and what’s its usage?
Thank you in advance and will be sure to vote up/accept answer
It is called function currying. The first bracket returns another function(lets call it: "myCustomFunc"). The 2nd bracket actually passes the 2nd value (number2) to the myCustomFunc.
Going off of this answer, the add_numbers function takes one argument (number1) and returns a function, which is then called with argument number2.

onclick function is undefined [duplicate]

This question already has an answer here:
Why does jsfiddle throw error that function is not defined? [duplicate]
(1 answer)
Closed 6 years ago.
I know this is probably something really dumb, but it really pisses me off for a good 15 minutes. What am I missing here?
<input type="button" value="Go" onclick="showAlert()">
function showAlert() {
alert('???');
}
Fiddle
Because in that fiddle you've chosen to define the function "onLoad".
That's same as defining it in window.onload=function(){...}, which makes it unaccessible outside the onload scope (i.e. you can call it only from that onload), as happens with any nested functions.
You should define that function in the <head>, choose the third option.

How to pass parameter to callback in javascript [duplicate]

This question already has answers here:
JavaScript: Passing parameters to a callback function
(16 answers)
Closed 6 years ago.
I would like to pass some parameter in call back like the following example. Is it a possible? Or I need to do it in another way?
function testCallback(callback, destination){
// how can I pass the destination to callback
callLib(callback)
}
function myCallback(destination){
//do something
}
callLib is an external library that accept. I guess the interface should be like this
function callLib(callback){
callback("something")
}
this one I think is not duplicate one. Sorry for my bad english. I have already change the question. see if you understand what the problem is
I played with callbacks all day to learn how to do this. Here is some example code: https://stackoverflow.com/questions/37554672/jqueryui-dialog-doesnt-stop-code-to-wait-for-user-input/37556573#37556573

! character before Self-Invoking Anonymous Function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the exclamation mark do before the function?
I;ve just came across a tablesorter plugin, and while looking at the source, I found that plugin is set up with Self-Invoking Anonymous Function - nothing unusual. Yet, I don't know what is the purpose of leading ! character:
!(function($) {})(jQuery);
I mean, how does it differ from this:
(function($) {})(jQuery);
For all intents and purposes, it doesn't differ at all. It negates the return from the function call, but since the return value isn't assigned to anything then it's pointless.

Categories