赋值 (=)

赋值 (=) 运算符用于将值分配给变量或属性。赋值表达式本身有一个值,即被赋值的值。这允许链接多个赋值,以便将单个值分配给多个变量。

¥The assignment (=) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables.

Try it

语法

¥Syntax

js
x = y

参数

¥Parameters

x

有效的分配目标,包括 identifier属性访问器。也可以是 解构赋值模式

y

指定要分配给 x 的值的表达式。

返回值

¥Return value

y 的值。

¥The value of y.

例外情况

¥Exceptions

ReferenceError

如果分配给未在范围内声明的标识符,则在严格模式下抛出。

TypeError

如果分配给 不可修改的属性,则会在严格模式下抛出。

描述

¥Description

赋值运算符与其他位置用作语法分隔符的等号(=)完全不同,其中包括:

¥The assignment operator is completely different from the equals (=) sign used as syntactic separators in other locations, which include:

所有这些地方都接受 = 右侧的赋值表达式,因此如果你有多个等号链接在一起:

¥All these places accept an assignment expression on the right-hand side of the =, so if you have multiple equals signs chained together:

js
const x = y = 5;

这相当于:

¥This is equivalent to:

js
const x = (y = 5);

这意味着 y 必须是一个预先存在的变量,而 x 是一个新声明的 const 变量。y 被赋予值 5x 使用 y = 5 表达式的值(也是 5)进行初始化。如果 y 不是预先存在的变量,则会在 非严格模式 中隐式创建全局变量 y,或者在严格模式下抛出 ReferenceError。要在同一声明中声明两个变量,请使用:

¥Which means y must be a pre-existing variable, and x is a newly declared const variable. y is assigned the value 5, and x is initialized with the value of the y = 5 expression, which is also 5. If y is not a pre-existing variable, a global variable y is implicitly created in non-strict mode, or a ReferenceError is thrown in strict mode. To declare two variables within the same declaration, use:

js
const x = 5,
  y = 5;

示例

¥Examples

简单的赋值和链接

¥Simple assignment and chaining

js
let x = 5;
let y = 10;
let z = 25;

x = y; // x is 10
x = y = z; // x, y and z are all 25

赋值表达式的值

¥Value of assignment expressions

赋值表达式本身计算为右侧的值,因此你可以记录该值并同时分配给变量。

¥The assignment expression itself evaluates to the value of the right-hand side, so you can log the value and assign to a variable at the same time.

js
let x;
console.log(x); // undefined
console.log(x = 2); // 2
console.log(x); // 2

不合格的标识符分配

¥Unqualified identifier assignment

全局对象位于作用域链的顶部。当尝试将名称解析为值时,将搜索作用域链。这意味着全局对象上的属性可以从每个范围方便地可见,而不必使用 globalThis.window.global. 限定名称。

¥The global object sits at the top of the scope chain. When attempting to resolve a name to a value, the scope chain is searched. This means that properties on the global object are conveniently visible from every scope, without having to qualify the names with globalThis. or window. or global..

因为全局对象有一个 String 属性(Object.hasOwn(globalThis, "String")),所以可以使用下面的代码:

¥Because the global object has a String property (Object.hasOwn(globalThis, "String")), you can use the following code:

js
function foo() {
  String("s"); // The function `String` is globally available
}

因此最终将在全局对象中搜索不合格的标识符。你不必输入 globalThis.String;你只需输入不合格的 String 即可。为了使此功能在概念上更加一致,如果作用域链中没有声明同名变量,则对非限定标识符的赋值将假定你要在全局对象上创建具有该名称的属性(省略 globalThis.)。

¥So the global object will ultimately be searched for unqualified identifiers. You don't have to type globalThis.String; you can just type the unqualified String. To make this feature more conceptually consistent, assignment to unqualified identifiers will assume you want to create a property with that name on the global object (with globalThis. omitted), if there is no variable of the same name declared in the scope chain.

js
foo = "f"; // In non-strict mode, assumes you want to create a property named `foo` on the global object
Object.hasOwn(globalThis, "foo"); // true

严格模式 中,在严格模式下分配给非限定标识符将导致 ReferenceError,以避免在全局对象上意外创建属性。

¥In strict mode, assignment to an unqualified identifier in strict mode will result in a ReferenceError, to avoid the accidental creation of properties on the global object.

请注意,上述内容的含义是,与流行的错误信息相反,JavaScript 没有隐式或未声明的变量。它只是将全局对象与全局范围合并在一起,并允许在属性创建期间省略全局对象限定符。

¥Note that the implication of the above is that, contrary to popular misinformation, JavaScript does not have implicit or undeclared variables. It just conflates the global object with the global scope and allows omitting the global object qualifier during property creation.

带有解构的赋值

¥Assignment with destructuring

的左侧也可以是分配模式。这允许一次分配给多个变量。

¥The left-hand side of can also be an assignment pattern. This allows assigning to multiple variables at once.

js
const result = /(a+)(b+)(c+)/.exec("aaabcc");
let a = "",
  b = "",
  c = "";
[, a, b, c] = result;
console.log(a, b, c); // "aaa" "b" "cc"

欲了解更多信息,请参阅 解构赋值

¥For more information, see Destructuring assignment.

规范

Specification
ECMAScript Language Specification
# sec-assignment-operators

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看