Function and variable with the same name

后端 未结 2 684
刺人心
刺人心 2020-11-30 03:23

The following code-snippet is a test to see what happens when a function and a variable share the same name in the same scope. In Chrome it appears the variable definition h

2条回答
  •  爱一瞬间的悲伤
    2020-11-30 03:48

    In JavaScript, function definitions are hoisted to the top of the current scope. Your example code therefore reads as:

    var overlapping = function() { return 'this is a function definition' };
    var overlapping = function() { return 'this is a var holding an anonymous function' };
    

    This is some good read about this topic: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

提交回复
热议问题