+ 3
why declare java script function in this way?
why do we declare function in this way. first declare a variable then declare a function against the variable. var return_first = function () { var tmp = null; $.ajax({ 'async': false, 'type': "POST", 'global': false, 'dataType': 'html', 'url': "ajax.php?first", 'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' }, 'success': function (data) { tmp = data; } }); return tmp; }();
1 Answer
0
The javascript language has something called block scope,
If you declared a function like: function myFunc() {} it .stays there, the problem is you can't access that function from inside of that function or above it,
However the var keyword in javascript get hoisted to the top of your code when you run it. So if you declare a function inside a variable, it will get hoisted to the top of your code and you won't be run into errors later on.
Hope this helps!
O White