Declare constants

Declare constants

Declare constants

Function

constants are used for bindings that are not allowed to be written again after saving the definition. They are suitable for configuration items, fixed identifiers, mathematical constants and function or class references that will not change.

Syntax

Naming rules

RulesDescription
The first lettermust be uppercase English letters.
The following characterscan be English letters, numbers or _.
CaseCase-sensitive, Name and NAME are two different constants.

Name, Config, NAME, and Name_1 are all constant names; User$ is not a legal constant name because the constant name cannot contain $.

Return Value

TypeDescription
AnyConstant definition expression returns the value of the expression on the right.

Code Examples

Notes

  • Constants cannot be defined repeatedly; an error will be reported the second time written in the same scope.
  • Do not use let for constants; let is only used for ordinary variables, and let Name = 1 will report an error according to the variable naming rules.
  • Global constants exist in the current VM for a long time. Constants with the same name cannot be defined within functions, nor can constants with the same name as existing global variables be defined.
  • The constant defined for the first time in a function belongs to the local scope of the function and is destroyed after the function call is completed; local constants with the same name can be defined in different functions.
  • The constant name only protects the binding itself; if the constant holds an object or array, whether to modify its internal fields depends on the object or array method itself.