简体   繁体   中英

Why does this javascript function executes by itself

I am having trouble understanding, what is making this function execute itself. Could somebody please explain it ? thanks

var iCallMySelf = function(){
console.log("Hi there :D");
}
(function(){})

This is a function

function(){
console.log("Hi there :D");
}

If you assigned this function to a variable, you could then execute it - this is the most normal way:

foo = function(){
console.log("Hi there :D");
}
foo()

However, you can use those brackets at the end to execute the function directly when you declare it. You have to wrap it up in brackets to make it a statement for this to work:

(function(){
console.log("Hi there :D");
})()

Assignment is also a statement, so this works to execute the function, too:

var iCallMySelf = function(){
console.log("Hi there :D");
}()

Finally, when you execute it, you can pass in arguments in those brackets at the end:

var iCallMySelf = function(){
console.log("Hi there :D");
}(1, 2, 3)

Or, as in your example, you're passing in another function as an argument:

var iCallMySelf = function(){
console.log("Hi there :D");
}
(
    // This is a function being passed as an argument to the function above
    function(){}
)

You can do stuff with that arg you pass in like this:

var iCallMySelf = function(foo){
console.log("Hi there :D");
// foo is the function you pass in below
console.log(foo)
}
(
    // This is a function being passed as an argument to the function above
    function(){}
)

Hope that helps!

If you don't want to call it automatically you have to put ; like

var iCallMySelf = function(){
console.log("Hi there :D");
};
(function(){})

due to wrapping of function below it get call automatically

whenever u put () after a function it is called so here if u use semicolon the next (function(){}) is not used by iCallMyself

but if u do like this

var myFun = function()
{
    console.log('Hii');
};
var x = myFun();

it will be called and Hi will be printed so, the same was in your code. Hope You Understand

similarily it will also be called

var myFun = function()
{
    console.log('Hii');
};
var x = myFun(function(){});

and in your question u used the same ,, so when i write function while defining xi can do this

var x = function(){console.log('Hii')}(function(){});

this is how your question was made... in your question iCallMyself is not the function to print 'Hi there :D' but its working as a variable and also it doesn't hold the returning value of function so to provide value to iCallMyself your function should be called otherwise it will not get any value,,

the iCallMyself will remain undefined even if its not a function u can try this

var iCallMySelf = function(){
console.log("Hi there :D");
}
(function(){})
console.log(iCallMyself);

just its a illusion of javascript that we think iCallMyself is a function but here its not

also in your question if u call iCallMyself(); u will get error because its not a function

like

try this

var iCallMySelf = function(){
console.log("Hi there :D");
}
(function(){})

iCallMyself();

it will give error that undefined is not a function because it is not a function it only seems to be but not really is.

So what's the logic of putting semicolon ;

when u put semicolon

var iCallMySelf = function(){
console.log("Hi there :D");
};
(function(){})
iCallMyself();

Now the line (function(){}) is not related to iCallMyself it is a wrapped function and nothing else now u can call iCallMyself() function

var x = function(){}();

so now function(){}() is executable whenever u return it to a variable

var x = function(){};

and function(){} is not executable by the var, the var will hold the function

var x = function(){};() 

and function(){};() now () is not necessary

You're passing a blank parameter. It would be like calling iCallMySelf(parameter) . Just imagine that after you declare a function and you add something to the end it is like you are sending it a set of parameters you want to start it with.

If you call it later iCallMySelf() it will do it again. If you iCallMySelf(blank) it will still do it. In javascript no parameters are required for a function and any defined parameters are still optional, but should be given a value if empty.

This is parsed as

var iCallMySelf = function(){console.log("Hi there :D");} ( function(){} );
//                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^
//                    a function expression                and another one
//                                                       ^^^            ^^
//                               a call of the first function, passing the second

Always use semicolons!

var iDontCallMySelf = function(){
  console.log("Hi there :D");
};
(function(){});

Or at least use function declarations:

function iDontCallMySelf(){
  console.log("Hi there :D");
}
(function(){});

If you want to create a self executing function in javascript it would look like this:

(function(param1, param2, ..) {
    // some code ..
})(param1, param2, ..);

Your code contains a self executing function since it's followed by () :

var iCallMySelf = function() {
    console.log("Hi there :D");
}(function(){})

The function(){} in ( ) is an anonymous function that is passed as parameter to the iCallMySelf function.

If you don't want to call that function add a ; after } :

var iCallMySelf = function() {
    console.log("Hi there :D");
};

(function(){})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM