which and how javascript function will be called if we have 2 function declarations with the same name?

前端 未结 6 1444
旧巷少年郎
旧巷少年郎 2021-01-14 04:58

Take a test:


6条回答
  •  礼貌的吻别
    2021-01-14 05:20

    It's because the latter function overwrites the previous one. If you try to log the function:

    console.log(say);
    

    It will return only:

    function say() { alert( "BELOW" ); } 
    

    The "ABOVE" alert has been replaced with the "BELOW" alert. Similar to declaring a variable with the same name twice in the same scope - the latter would overwrite the former.

    JSFiddle example.

    Why? See http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

提交回复
热议问题