In the following code example, using traditional named functions and anonymous functions does the same thing.
I read var functionName = function () {} vs function functionName () {} and understood how the first named function was defined at runtime, while the second anonymous function was defined at the time of parsing for the script block.
My question is specifically what conditions will make one approach more suitable than another?
function get1() { return 'Hello'; }; function alert1(data) { alert(data); }; var get2 = function() { return 'Goodby'; }; var alert2 = function(data) { alert(data); }; alert1(get1()); alert2(get2());
, function logfoo function logbar return, , , / return.
function logfoo
function logbar
var myModule = (function() { return { foo: function() { logfoo(); }, bar: function() { logbar(); } } function logfoo() { console.log('foo'); } function logbar() { console.log('bar'); } })();
, , doesnt . .
:
Source: https://habr.com/ru/post/1526378/More articles:4th argument to glDrawElements - WHAT? - c ++What is the simple Javascript equivalent of .each and $ (this) when used together, as in this example? - javascriptREST API authentication: how to prevent man-in-the-middle repeats? - authenticationWhy is my Clojure project running slowly on raspberries? - clojureMule - remote SFTP archiving - muleupload files from an external url and transfer the file to the user directly without saving it to my server. - urlEclipse - link file with editor without extension - code-formattingAny problems with reselling a self-service SSL certificate WHM? - ssl-certificateRspec match_array error for empty array? - rspecPartial Response Recommendations with Spring HATEOAS - javaAll Articles