Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I name a JavaScript function and execute it immediately?

I have quite a few of these:

function addEventsAndStuff() {   // bla bla } addEventsAndStuff();  function sendStuffToServer() {   // send stuff   // get HTML in response   // replace DOM   // add events:   addEventsAndStuff(); } 

Re-adding the events is necessary because the DOM has changed, so previously attached events are gone. Since they have to be attached initially as well (duh), they're in a nice function to be DRY.

There's nothing wrong with this set up (or is there?), but can I smooth it a little bit? I'd like to create the addEventsAndStuff() function and immediately call it, so it doesn't look so amateuristic.

Both following respond with a syntax error:

function addEventsAndStuff() {   alert('oele'); }();  (function addEventsAndStuff() {   alert('oele'); })(); 

Any takers?

like image 874
Rudie Avatar asked Jun 19 '11 18:06

Rudie


People also ask

How do you call a function immediately in JavaScript?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog. (function () { // … }) (); (() => { // … })

Can you call a function before declaring it JavaScript?

So generally html - including javascript - has a top down method of parsing code. However, functions go out of this route as they are read first, but only the function head (= first line). That is why function calls are possible before declaration.

How do you call a JavaScript function name?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

Is it mandatory to call the function in JS to get that executed?

Javascript functions are only executed when you explicitly call them (or implicitly in callbacks and whatnot). The code will however still be interpreted by the browser on each page regardless of functions being called or not.


1 Answers

There's nothing wrong with the example you posted in your question.. The other way of doing it may look odd, but:

var addEventsAndStuff; (addEventsAndStuff = function(){     // add events, and ... stuff })(); 

There are two ways to define a function in JavaScript. A function declaration:

function foo(){ ... } 

and a function expression, which is any way of defining a function other than the above:

var foo = function(){}; (function(){})(); var foo = {bar : function(){}}; 

...etc

function expressions can be named, but their name is not propagated to the containing scope. Meaning this code is valid:

(function foo(){    foo(); // recursion for some reason }()); 

but this isn't:

(function foo(){     ... }()); foo(); // foo does not exist 

So in order to name your function and immediately call it, you need to define a local variable, assign your function to it as an expression, then call it.

like image 62
Nobody Avatar answered Sep 30 '22 18:09

Nobody