What is the difference between these two?

This question already has an answer here:

  • var functionName = function() {} vs function functionName() {} 32 answers
  • Are named functions or anonymous functions preferred in JavaScript? [duplicate] 4 answers

  • This has been answered many times in StackOverflow. It is just the way of naming. So taking up some points from the answers, I would say:

  • Function declarations and variable declarations are always moved ("hoisted") invisibly to the top of their containing scope by the JavaScript interpreter. Function parameters and language-defined names are, obviously, already there.

  • Advantages & Disadvantages:

    There are few advantages to naming functions:

  • names for meta analysis. functionInstance.name will show you the name.
  • Far more importantly, the name will be printed in stack traces.
  • names also help write self documenting or literate code.
  • There is a single disadvantage to named functions expressions

  • IE has memory leaks for NFE
  • Another main difference

    The difference is that functionTwo is defined at parse-time for a script block, whereas functionOne is defined at run-time. For example:

    <script>
      // Error
      functionOne();
    
      var functionOne = function() {
      }
    </script>
    
    <script>
      // No error
      functionTwo();
    
      function functionTwo() {
      }
    </script>
    
  • References

  • var functionName = function() {} vs function functionName() {}
  • Are named functions or anonymous functions preferred in JavaScript?
  • Named function expressions demystified
  • Function Declarations vs. Function Expressions.
  • var functionName = function() {} vs function functionName() {}

  • 1st one is Named Function Expressions, which should return some value to the caller.
  • 2nd one is just a function, it's upto you whether you return value or not
  • 链接地址: http://www.djcxy.com/p/96288.html

    上一篇: var NAME = function NAME(){};

    下一篇: 这两者有什么区别?