Javascript 中的 const 关键字作用域

2024-04-19前端开发问题
10

本文介绍了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 关键字作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5