javascript 常量

JavaScript常数 (JavaScript Constants)

Before ES15, the only way to declare variables using the var keyword. JavaScript's inbuilt feature of hoisting variables could be carried out using the var keyword. If you're unfamiliar with variables in JavaScript, have a glance at Variables in JavaScript article on the website. If you wish to know what hoisting is, read through Hoisting in JavaScript.

在ES15之前,这是使用var关键字声明变量的唯一方法。 JavaScript的内置变量提升功能可以使用var关键字执行。 如果您不熟悉JavaScript中的变量,请浏览网站上的JavaScript中的变量文章。 如果您想知道什么是提升,请通读JavaScript中的提升 。

Today we'll look at constants. Variables after ES15 could be declared in two ways - let and const. Before we dive deeper into const, let's understand what a constant is?

今天我们来看一下常量 。 ES15之后的变量可以两种方式声明: letconst 。 在深入研究const之前,让我们了解一个常数是什么?

Constants in most languages are something that retains their value during their block or wherever their life persists. In JavaScript, constants are values that we cannot modify later directly. This means if we declare a constant with a certain value, assigning some other value later in the program would result in an error.

大多数语言中的常量在其阻塞期间或生命持续存在的地方都可以保留其价值。 在JavaScript中,常量是我们以后无法直接修改的值。 这意味着,如果我们声明一个具有特定值的常数,则稍后在程序中分配其他一些值将导致错误。

    const a=10;
a=25;

Output

输出量

    Uncaught TypeError: Assignment to constant variable. at <anonymous>:1:2

We get a TypeError.

我们得到一个TypeError

We declare a constant using the keyword const.

我们使用关键字const声明一个常量。

const follows block scope. Consider the following,

const遵循块作用域。 考虑以下,

var t=10; //Value of t here is 10
{const t=12;    //Value of t here is 12
}
//Value of t here is 10

Also, constants declared using the const keyword must be initialized and declared at the same time.

另外, 使用const关键字声明的常量必须同时初始化和声明

    const g=9.8;
//is correct, but
const g;
g=9.8i
//is incorrect.

However, an important catch to note that the const keyword is not what it looks like. The more accurate statement regarding const would be that it defines a constant reference to a value. This means that primitive values assigned to a variable using the const keyword cannot be altered but with objects, we are bound to no such restriction.

但是,需要注意的重要一点是const关键字不是它的外观。 关于const的更准确的陈述是,它定义了对值常量引用 。 这意味着使用const关键字分配给变量的原始值不能更改,但是对于对象,我们不受任何限制。

If we declare an object with the const keyword, we can later change it by giving it new properties.

如果我们使用const关键字声明一个对象,我们以后可以通过为其赋予新属性来对其进行更改。

    const person={
Name: 'Fuzzy Sid',
Age: 20
}
person.gender='Male';

Adding a new property to a constant object does not yield us an error.

向常量对象添加新属性不会产生错误。

Similarly, we can change the elements of an array defined using the const keyword.

同样, 我们可以更改使用const关键字定义的数组的元素

    const arr=[1,2,3,4];
console.log(arr);       //[1,2,3,4]
arr[0]=0;
console.log(arr);       //[0,2,3,4]
arr.push_back(5);
console.log(arr);       //[0,2,3,4,5]

However, we cannot reassign new values to the array.

但是, 我们无法将新值重新分配给array

    arr = [9,8,7,6]

Output

输出量

Uncaught TypeError: Assignment to constant variable.at <anonymous>:1:4

Would give an error. But the trick around this would be to individually change the values of the array or maybe run a loop for the same.

会给出一个错误。 但是,解决这个问题的技巧是单独更改数组的值,或者为该数组运行一个循环。

    for(let i=0,cnt=9; i<arr.length; i++,cnt--){
arr[i]=cnt;
}

Remember that const variables are not supported in versions of Internet Explorer before 10 and they are not hoisted. So you cannot use them before the declaration.

请记住,Internet Explorer 10之前的版本不支持const变量,也不会使用它们。 因此,您不能在声明之前使用它们。

翻译自: https://www.includehelp.com/code-snippets/constants-in-javascript.aspx

javascript 常量

javascript 常量_JavaScript中的常量相关推荐

  1. java定义常量_JAVA中定义常量方法

    JAVA中定义常量方法 (2013-01-28 14:30:19) 标签: it 一.常量定义的基本注意事项. 在Java语言中,主要是利用final关键字(在Java类中灵活使用Static关键字) ...

  2. java中常量_Java中的常量有哪些?

    JAVA常量就是在程序中固定不变的值,是不能改变的数据.例如数字1.字符"a".浮点数3.2等.那么java的常量有哪些呢?在Java中,常量包括整型常量.浮点数常量.布尔常量.字 ...

  3. javascript函数式_JavaScript中的函数式编程原理

    javascript函数式 After a long time learning and working with object-oriented programming, I took a step ...

  4. java中属于常量_java中的常量和属性

    Java最佳实践建议将属性作为常量读取.那么,您认为达到目标的最佳方法是什么?我的方法是:一个Configuration类只读取一次属性文件(单例模式),并使用此类在需要时读取属性作为常量.并存储一个 ...

  5. javascript函数式_JavaScript中的函数式编程—结合实际示例(第1部分)

    javascript函数式 by rajaraodv 通过rajaraodv JavaScript中的函数式编程-结合实际示例(第1部分) (Functional Programming In Jav ...

  6. javascript 解密_Javascript中的AES加密和Java中的解密

    javascript 解密 AES代表高级加密系统,它是一种对称加密算法,很多时候我们需要在客户端加密一些纯文本,例如密码,然后将其发送到服务器,然后由服务器解密以进行进一步处理.AES加密和解密更加 ...

  7. javascript运算符_JavaScript中的按位运算符

    javascript运算符 JavaScript按位运算符 (JavaScript Bitwise Operators) A lot of times you come across some str ...

  8. javascript 排序_JavaScript中的排序方法

    javascript 排序 There are tons of sorting algorithms available like bubble sort, merge sort, insertion ...

  9. javascript运算符_JavaScript中!=或!==运算符之间的区别

    javascript运算符 We can perceive the differences between these two operators as the same difference tha ...

最新文章

  1. 1024程序员节,你是我们要找的那条锦鲤吗?
  2. php foreach方法,forEach方法怎么使用
  3. Struts框架原理分析之我见
  4. python模式匹配算法_详解Python 最短匹配模式
  5. 如何通过页面静态化提升论坛性能
  6. Kubernetes核心技术--Pod,Label,Volume,Service,Deployment详解
  7. 域服务器安全策略应用,ad域服务器组策略命令
  8. 东南大学计算机技术935专业考研经验分享
  9. html文本框的文字间距,word文本框中2行文字的间距为什么那么大
  10. 【腾讯TMQ】我们在外包资源池化管理走过的弯路
  11. 阿里服务器微信发不了图片,为什么微信发不了图片?这四招教你解决难题
  12. 为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
  13. IntelliJ Idea 常用快捷键列表
  14. kubernetes系列之五:IPVS概览
  15. 河南太康一高高考成绩查询2021,河南周口2020高考喜报,太康一高一本上线增幅巨大,值得重点关注...
  16. python架构师是做什么的_【图片】架构师速成-一个10多年架构师的总结_架构师吧_百度贴吧...
  17. 认识Linux瘦客户机
  18. 网卡驱动程序不正常上不了网的修复方法
  19. 图文并茂raid技术详解(raid大全)
  20. 使用Google Voice打造车载语音交互

热门文章

  1. 从JS敏感信息泄露到GETSHELL
  2. springboot中得注解_Spring以及SpringBoot中的常用的注解小结
  3. 云桌面 瘦终端_小米盒子连接Citrix云桌面
  4. http 请求报文和响应报文
  5. 51Nod - 1381 硬币游戏
  6. mxnet系列教程之1-第一个例子
  7. @Resource VS @Autowired
  8. STL中的lower_bound和upper_bound的理解
  9. 介绍针对企业级Flex开发的开源项目FlexibleShare
  10. 装修月记第一弹,硬装篇