如果我们有两个具有相同名称的函数声明,那么将调用哪些javascript函数? [英] which and how javascript function will be called if we have 2 function declarations with the same name?

查看:120
本文介绍了如果我们有两个具有相同名称的函数声明,那么将调用哪些javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接受测试:

Take a test:

<script>
    function say() { alert( "ABOVE" ); }

    say();

    function say() { alert( "BELOW" ); }
</script>




  • 对于所有测试(Chrome,Firefox, IE)。

  • 在这种情况下JavaScript解释器是如何工作的?

  • http://jsfiddle.net/jcv6l/ <<运行代码。

    • The result is "BELOW" for all test (Chrome, Firefox, IE).
    • How does javascript interpreter work in this case?
    • http://jsfiddle.net/jcv6l/ << run the code.
    • 推荐答案

      基本上,由于提升,将所有函数声明拉到解释器基本上是这样做的:

      Basically, because of hoisting, which pulls all function declarations to the top of the current scope, the interpreter is basically doing this:

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

      这就是为什么它总是在 >

      That is why it always ends up alerting below

      这篇关于如果我们有两个具有相同名称的函数声明,那么将调用哪些javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆