What does this javascript code mean?

var myval = (function(){})();

I do not understand the code (function..)and even the code yet.

+3
source share
6 answers

You have:

self-running anonymous function

First, you create a function expression by positioning a paret around the function itself. Just write

function() {
}()

will not work on this instance because it will determine the function declaration.

So, after that we can call ourselves by adding()

(function() {
})();

To verify this, try the following:

var myval = (function(){return 'self executed!'})();

alert(myval); // === 'self executed'
+8
source
  • function(){} is an expression of a function, it defines a function
  • (function(){}) - wrapping it in this way ensures that it will be treated as an expression
  • (function(){})()- Addition ()calls function

.

, .

+8

.

(function ($) {
  // Original JavaScript code.
})(jQuery);

$ , jQuery.

+2

function(){} () . (()), . :

var f = function() {};
f();

?

(function(x, y, z){})(1, 2, 3)
+1

:

(.. )

function(){}

, {}.

,

myval = function(){<something>};

myval (, !)

, myval(), , .

, () . :

var myval = (function(){})();

( ) myval

0

All Articles