<i id='iE4YS'><tr id='iE4YS'><dt id='iE4YS'><q id='iE4YS'><span id='iE4YS'><b id='iE4YS'><form id='iE4YS'><ins id='iE4YS'></ins><ul id='iE4YS'></ul><sub id='iE4YS'></sub></form><legend id='iE4YS'></legend><bdo id='iE4YS'><pre id='iE4YS'><center id='iE4YS'></center></pre></bdo></b><th id='iE4YS'></th></span></q></dt></tr></i><div id='iE4YS'><tfoot id='iE4YS'></tfoot><dl id='iE4YS'><fieldset id='iE4YS'></fieldset></dl></div>

    • <bdo id='iE4YS'></bdo><ul id='iE4YS'></ul>

  1. <tfoot id='iE4YS'></tfoot>

    <small id='iE4YS'></small><noframes id='iE4YS'>

    1. <legend id='iE4YS'><style id='iE4YS'><dir id='iE4YS'><q id='iE4YS'></q></dir></style></legend>

      Javascript 中的 const 关键字作用域

      const keyword scope in Javascript(Javascript 中的 const 关键字作用域)

      <small id='LwNgi'></small><noframes id='LwNgi'>

      <legend id='LwNgi'><style id='LwNgi'><dir id='LwNgi'><q id='LwNgi'></q></dir></style></legend>

      • <bdo id='LwNgi'></bdo><ul id='LwNgi'></ul>

        <tfoot id='LwNgi'></tfoot>
          <i id='LwNgi'><tr id='LwNgi'><dt id='LwNgi'><q id='LwNgi'><span id='LwNgi'><b id='LwNgi'><form id='LwNgi'><ins id='LwNgi'></ins><ul id='LwNgi'></ul><sub id='LwNgi'></sub></form><legend id='LwNgi'></legend><bdo id='LwNgi'><pre id='LwNgi'><center id='LwNgi'></center></pre></bdo></b><th id='LwNgi'></th></span></q></dt></tr></i><div id='LwNgi'><tfoot id='LwNgi'></tfoot><dl id='LwNgi'><fieldset id='LwNgi'></fieldset></dl></div>

                <tbody id='LwNgi'></tbody>
                本文介绍了Javascript 中的 const 关键字作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                1. >>> const a = 2
                2. >>> var a = 3
                3. >>> a = 4
                4. >>> a // print 2
                

                为什么允许运行3号线?const 似乎比没有任何关键字更全局"...

                Why the operation line 3 is allowed? const seems more "global" than without any keyword...

                推荐答案

                这是 就是 const 的工作原理(或不起作用):

                This is is just how const works (or doesn't work):

                创建一个常量1,对于声明它的函数来说,它可以是全局的或局部的.常量遵循与变量相同的范围规则 [.. 并且不能与同一范围内的函数或变量共享名称].

                Creates a constant1 that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables [.. and cannot share a name] with a function or a variable in the same scope.

                如果您重新声明2 [与重新分配不同] 常量,则

                Firefox [..] 会引发 TypeError.如果您分配另一个值给常量 [..] ,所有主要浏览器都不会产生任何通知或错误2,3重新分配不成功(仅)在 Firefox 和 Chrome 中(至少从版本 20 开始).

                Firefox [..] throws a TypeError if you redeclare2 [which is different than re-assigning] a constant. None of the major browsers produce any notices or errors2,3 if you assign another value to a constant [..] but the reassignment is unsuccessful (only) in Firefox and Chrome (at least since version 20).

                请注意,const 不是 ECMAScript 5 规范的一部分,JavaScript 1.5 语义将在 ECMAScript 6 中重新定义.

                Note that const is not part of the ECMAScript 5 specification and the JavaScript 1.5 semantics will be re-defined in ECMAScript 6.

                行为在支持和重新声明/重新分配语义方面因浏览器实现而异.

                Behavior will vary across browser implementations with respect to support and re-declaration/re-assignments semantics.

                1 在 IE 9 中,使用 const a = 2 会导致

                1 In IE 9, using const a = 2 results in

                语法错误"

                2 在 FF 14 中,const a = 2;变量 a = 3;a = 4;a,当作为单个程序评估时,会导致

                2 In FF 14, const a = 2; var a = 3; a = 4; a, when evaluated as a single program, results in

                TypeError: 重新声明 const a

                TypeError: redeclaration of const a

                这与在 REPL 中一次执行每一行 不同.我怀疑这是因为var提升const之上,并且因为const不能与同一范围内的函数或变量".

                which is different than executing each line one-at-a-time in the REPL. I suspect this is because var is hoisted above the const and because a const "cannot share a name with a function or variable in the same scope".

                3 在 Chrome 21 中,const a = 2;变量 a = 3;a = 4;a 计算结果为 2,没有警告或消息.

                3 In Chrome 21, const a = 2; var a = 3; a = 4; a evaluates to 2 with no warning or message.

                这篇关于Javascript 中的 const 关键字作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                  <tbody id='REd8F'></tbody>
                  <tfoot id='REd8F'></tfoot>

                    <legend id='REd8F'><style id='REd8F'><dir id='REd8F'><q id='REd8F'></q></dir></style></legend>

                    <i id='REd8F'><tr id='REd8F'><dt id='REd8F'><q id='REd8F'><span id='REd8F'><b id='REd8F'><form id='REd8F'><ins id='REd8F'></ins><ul id='REd8F'></ul><sub id='REd8F'></sub></form><legend id='REd8F'></legend><bdo id='REd8F'><pre id='REd8F'><center id='REd8F'></center></pre></bdo></b><th id='REd8F'></th></span></q></dt></tr></i><div id='REd8F'><tfoot id='REd8F'></tfoot><dl id='REd8F'><fieldset id='REd8F'></fieldset></dl></div>
                    1. <small id='REd8F'></small><noframes id='REd8F'>

                          <bdo id='REd8F'></bdo><ul id='REd8F'></ul>