How to understand the functions and avoid confusion?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
14 views
Hi all. Help to understand function. I'm really confused with return, with closures, function expression and function declaration. All day trying to return a value from a function, going through different options. You seem to know how, in Google video and there are examples, but when you adjust to fit your needs - no longer works. I just don't fully understand the principles of functions and return.

here is the function and its call. Why doesn't work ?

$('#button').on('click', test); function test() { var sum = 10; var res = function() { console.log(sum); } return res; }; test();


But so works

function test() { var sum = 10; var res = function() { console.log(sum); } return res; }; var newTest = test(); $('#button').on('click', newTest);


I just added a function test() to the variable newTest and now the event click calls newTest

And here's how it works :

function test() { var sum = 10; var res = function() { console.log(sum); } return res; }; var newTest = test(); $('#button').on('click', test());


Now when you click I call test() with parentheses (unlike newTest - without brackets - in the previous example), but it is not triggered immediately, but only after clicking. Though, like, if I specify test() on the handler that should be invoked..

Here so does not work :

var test = function() { var sum = 10; var res = function() { console.log(sum); } return res; }; $('#button').on('click', test);


If you call it using : console.log(test()); that will return the description of the function res() (or reference variable this function);

And if you call console.log(test); without parentheses, it returns a description of the function test(). Well, here's something more or less clear that the result of the function test() is a function res(). If you simply call the test without the parentheses - returns a description of the test. With parentheses if the function will be executed, and its result is a description of the function res(). But what happened to the console.log(sum) ?

Here's another example. Here I do not specify the brackets and everything works. And works click function test().

function test() { var sum = 10; console.log(sum); }; $('#button').on('click', test);


Or here :

function test() { var sum = 10; console.log(sum); }; var newTest = test; $('#button').on('click', newTest);


Result is not important, I will call when you click test or newTest ?
Understand that it is better to use the second option ?

Like hypothetically understand how it returns, but when I try the function result set to a variable, then the variable to use for adding the amount with other variables - nothing happens.
Thanks in advance for answers
by | 14 views

1 Answer

0 like 0 dislike
You must clearly distinguish the concept of function and pointer to function.
If very simple, when you write this:
test();
you perform this function

and if so:
var newTest = test;
then you specify the function as an object, passing the function as an object (not the result) in a variable newTest
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
7 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users