What is the difference between these two?
This question already has an answer here:
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:
functionInstance.name
will show you the name. There is a single disadvantage to named functions expressions
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 NAME = function NAME(){};
下一篇: 这两者有什么区别?