Toán tử kết hợp Nullish [??
] là một toán tử logic trả về toán hạng bên phải của nó khi toán hạng bên tay trái của nó là null
hoặc
0, và nếu không thì sẽ trả lại toán hạng bên trái của nó.nullish coalescing operator [let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
??
] is a logical operator that returns its right-hand side operand when its left-hand side operand is null
or
0, and otherwise returns its
left-hand side operand. let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
Điều này có thể được coi là một trường hợp đặc biệt của toán tử logic hoặc [____11], trả về toán hạng bên phải nếu toán hạng bên trái là bất kỳ giá trị giả nào, không chỉ null
hoặc
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0. Nói cách khác, nếu bạn sử dụng let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1 để cung cấp một số giá trị mặc định cho một biến khác let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
5, bạn có thể gặp phải các hành vi không mong muốn nếu bạn coi một số giá trị giả là có thể sử dụng được [ví dụ: let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
6 hoặc let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
7]. Xem dưới đây để biết thêm ví dụ.Toán tử kết hợp nullish có toán tử thấp thứ năm, thấp hơn trực tiếp so với
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1 và cao hơn so với toán tử có điều kiện [ternary].Thử nó
Cú pháp
Ví dụ
Sử dụng nhà điều hành liên kết nullish
Trong ví dụ này, chúng tôi sẽ cung cấp các giá trị mặc định nhưng giữ các giá trị khác với null
hoặc
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0.const nullValue = null;
const emptyText = ""; // falsy
const someNumber = 42;
const valA = nullValue ?? "default for A";
const valB = emptyText ?? "default for B";
const valC = someNumber ?? 0;
console.log[valA]; // "default for A"
console.log[valB]; // "" [as the empty string is not null or undefined]
console.log[valC]; // 42
Gán giá trị mặc định cho một biến
Trước đó, khi người ta muốn gán giá trị mặc định cho một biến, một mẫu chung là sử dụng logic hoặc toán tử [
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1]:let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
Tuy nhiên, do
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1 là một toán tử logic boolean, toán hạng bên trái đã bị ép buộc để đánh giá và bất kỳ giá trị giả nào [let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
7, let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
6, const count = 0;
const text = "";
const qty = count || 42;
const message = text || "hi!";
console.log[qty]; // 42 and not 0
console.log[message]; // "hi!" and not ""
5, null
, let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0] không được trả lại. Hành vi này có thể gây ra hậu quả bất ngờ nếu bạn coi let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
7, let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
6 hoặc const count = 0;
const text = "";
const qty = count || 42;
const message = text || "hi!";
console.log[qty]; // 42 and not 0
console.log[message]; // "hi!" and not ""
5 là giá trị hợp lệ.const count = 0;
const text = "";
const qty = count || 42;
const message = text || "hi!";
console.log[qty]; // 42 and not 0
console.log[message]; // "hi!" and not ""
Toán tử kết hợp nullish tránh được cạm bẫy này bằng cách chỉ trả lại toán hạng thứ hai khi cái đầu tiên đánh giá thành null
hoặc
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0 [nhưng không có giá trị giả nào khác]:const myText = ''; // An empty string [which is also a falsy value]
const notFalsyText = myText || 'Hello world';
console.log[notFalsyText]; // Hello world
const preservingFalsy = myText ?? 'Hi neighborhood';
console.log[preservingFalsy]; // '' [as myText is neither undefined nor null]
Short-circuiting
Giống như các toán tử OR và và logic, biểu thức bên phải không được đánh giá nếu phía bên trái chứng minh là không null
cũng không
let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0.function A[] { console.log['A was called']; return undefined;}
function B[] { console.log['B was called']; return false;}
function C[] { console.log['C was called']; return "foo";}
console.log[A[] ?? C[]];
// logs "A was called" then "C was called" and then "foo"
// as A[] returned undefined so both expressions are evaluated
console.log[B[] ?? C[]];
// logs "B was called" then "false"
// as B[] returned false [and not null or undefined], the right
// hand side expression was not evaluated
Không có chuỗi với và hoặc các nhà khai thác
Không thể kết hợp cả và [
const myText = ''; // An empty string [which is also a falsy value]
const notFalsyText = myText || 'Hello world';
console.log[notFalsyText]; // Hello world
const preservingFalsy = myText ?? 'Hi neighborhood';
console.log[preservingFalsy]; // '' [as myText is neither undefined nor null]
5] và các toán tử [let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1] trực tiếp với ??
. Một const myText = ''; // An empty string [which is also a falsy value]
const notFalsyText = myText || 'Hello world';
console.log[notFalsyText]; // Hello world
const preservingFalsy = myText ?? 'Hi neighborhood';
console.log[preservingFalsy]; // '' [as myText is neither undefined nor null]
8 sẽ được ném trong những trường hợp như vậy.null || undefined ?? "foo"; // raises a SyntaxError
true || undefined ?? "foo"; // raises a SyntaxError
Tuy nhiên, việc cung cấp dấu ngoặc đơn để chỉ ra một cách rõ ràng ưu tiên là chính xác:
[null || undefined] ?? "foo"; // returns "foo"
Mối quan hệ với toán tử chuỗi tùy chọn [const myText = ''; // An empty string [which is also a falsy value]
const notFalsyText = myText || 'Hello world';
console.log[notFalsyText]; // Hello world
const preservingFalsy = myText ?? 'Hi neighborhood';
console.log[preservingFalsy]; // '' [as myText is neither undefined nor null]
9]
const myText = ''; // An empty string [which is also a falsy value]
const notFalsyText = myText || 'Hello world';
console.log[notFalsyText]; // Hello world
const preservingFalsy = myText ?? 'Hi neighborhood';
console.log[preservingFalsy]; // '' [as myText is neither undefined nor null]
Toán tử kết hợp nullish coi ____10 và null
là các giá trị cụ thể và toán tử chuỗi tùy chọn [
const myText = ''; // An empty string [which is also a falsy value]
const notFalsyText = myText || 'Hello world';
console.log[notFalsyText]; // Hello world
const preservingFalsy = myText ?? 'Hi neighborhood';
console.log[preservingFalsy]; // '' [as myText is neither undefined nor null]
9] cũng hữu ích để truy cập vào một thuộc tính của một đối tượng có thể là null
hoặc let foo;
// foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0.const foo = { someFooProp: "hi" };
console.log[foo.someFooProp?.toUpperCase[] ?? "not available"]; // "HI"
console.log[foo.someBarProp?.toUpperCase[] ?? "not available"]; // "not available"
Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # prod-coalescepression # prod-CoalesceExpression |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt