admin管理员组

文章数量:1335871

I am new in Javascript, but have deep background real OO languages like C#, Java, C++... In Javascript there is a concept called anonymous functions. Here is a sample code:

(   function() { 
      for(var x = 0;x<5;x++) {
         console.log(x); 
      } 
   })(); 

As I have understood the parantheses at the end make the function call itself. There is also another syntax which does the same:

var x =   function() { 
      for(var x = 0;x<5;x++) {
         console.log(x); 
      } 
   }(); 

But right now if I try to use x, it does not execute the function again. So what is the goal if using the assignment in the second version? Can I use the function via x again?

I am new in Javascript, but have deep background real OO languages like C#, Java, C++... In Javascript there is a concept called anonymous functions. Here is a sample code:

(   function() { 
      for(var x = 0;x<5;x++) {
         console.log(x); 
      } 
   })(); 

As I have understood the parantheses at the end make the function call itself. There is also another syntax which does the same:

var x =   function() { 
      for(var x = 0;x<5;x++) {
         console.log(x); 
      } 
   }(); 

But right now if I try to use x, it does not execute the function again. So what is the goal if using the assignment in the second version? Can I use the function via x again?

Share Improve this question edited Nov 6, 2017 at 7:59 Derek 朕會功夫 94.4k45 gold badges197 silver badges253 bronze badges asked Nov 6, 2017 at 7:51 Code PopeCode Pope 5,4598 gold badges31 silver badges77 bronze badges 5
  • 3 Since you have () after function, it will call it immediately and assign its return value (in your case, undefined). Remove () and if you want to call it immediately, make a call on next line like: x(); – Rajesh Commented Nov 6, 2017 at 7:54
  • 1 Beware that JavaScript is not object oriented. Instead, JS is prototype oriented and there will be things that surprise you. – Derek 朕會功夫 Commented Nov 6, 2017 at 7:58
  • 2 why someone downvoted this question? it is a good question for beginners – Luke Commented Nov 6, 2017 at 7:59
  • The first one without var x = is actually an IIFE stackoverflow./questions/8228281/… – Niladri Commented Nov 6, 2017 at 8:03
  • 1 var x is the result of calling that anonymous function ... which, in this case ... is undefined as it has no return – Jaromanda X Commented Nov 6, 2017 at 8:06
Add a ment  | 

3 Answers 3

Reset to default 5

Self executing function are known as IIFE (Immediately-Invoked Function Expression), it is usually used to control the scoping so you don't end up with a lot of global variables.

For example, this function act as a moneybox, it encapsulate all information of your "money", so you can only insert money or get the total money, but you can't directly call add/get and access the variable.

It can be used as a form of OOP as well, since you are already very familiar with it

var myMoneyBox = (function() {
    var money = 0;

    function addMoney(x) {
        if (x > 0)
            money += x;
    }

    function getMoney() {
        return money;
    }

    return {
        add: addMoney,
        get: getMoney
    }
})();

myMoneyBox.add(10);
console.log(myMoneyBox.get());

x is assigned the result of your function, just like in any other expression x = f(), you just define f within the expression. It doesn't have a return value so in this case the value of x is undefined.

If you want to use x to call the function, then just don't add the () to the end of the expression...

I think this could help you:

var x =   function() { 
      for(var x = 0;x<5;x++) {
         console.log(x); 
      } 
   }; 
x();

本文标签: About Anonymous Functions in JavascriptStack Overflow