如何在JavaScript中声明字符串常量? [英] How to declare string constants in JavaScript?

查看:47
本文介绍了如何在JavaScript中声明字符串常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaScript中声明字符串常量。

I want to declare string constants in JavaScript.

有办法吗?

推荐答案

许多浏览器的实现(和Node)都有常量,可与 const

Many browsers' implementations (and Node) have constants, used with const.

const SOME_VALUE = "Your string";

const 表示您不能将其重新分配为其他任何值。

This const means that you can't reassign it to any other value.

检查兼容性说明,以查看您的目标浏览器是否受支持。

Check the compatibility notes to see if your targeted browsers are supported.

或者,您也可以修改第一个示例,使用 defineProperty () 或其朋友,并将可写属性设置为 false 。这将意味着变量的内容不能更改,就像常量一样。

Alternatively, you could also modify the first example, using defineProperty() or its friends and make the writable property false. This will mean the variable's contents can not be changed, like a constant.

这篇关于如何在JavaScript中声明字符串常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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