Làm cách nào để gán nhiều giá trị cho một biến trong JavaScript?

Hướng dẫn này giải thích cách gán nhiều biến trong JavaScript vì biến là phần quan trọng nhất trong quá trình viết mã của chúng ta

Đôi khi, chúng ta phải khai báo và gán nhiều biến vì chúng có cùng giá trị. Làm sao?

Sử dụng Toán tử = để gán nhiều biến trong JavaScript

Giả sử chúng ta có variable1, variable2variable3 và muốn cả ba biến có giá trị là 1

var variable1 = 1, variable2 = 1, variable3 = 1;
console.log(variable1,variable2,variable3); //1,1,1

var variable1 = variable2 = variable3 = 1;
console.log(variable1,variable2,variable3); //1,1,1

đầu ra

Chúng có vẻ tương đương, nhưng chúng không. Lý do là biến '_______26_______ và gán ____1_______0

Toán tử gán là

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
1 trong JavaScript, có nghĩa là nó phân tích cú pháp ngoài cùng bên trái sau khi phân tích cú pháp ngoài cùng bên phải

Hãy lấy một ví dụ khác để hiểu biến scope và phép gán

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
0

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!

đầu ra

Tập trung vào mã và thấy rằng variable1, variable2variable3 nằm trong phạm vi chức năng và cục bộ đối với

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
7

Chúng không có sẵn bên ngoài phương pháp

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
7, đó là lý do tại sao trả lại
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
9. Ở đây,
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
0 tương đương với
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
1

Bây giờ, hãy quan sát chức năng

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
2. variable1 nằm trong phạm vi chức năng do từ khóa
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
4, nhưng variable2variable3 bị rò rỉ vì chúng không được viết bằng từ khóa
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
4

Chúng có thể truy cập toàn cầu bên ngoài chức năng

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
2. Hãy nhớ rằng các khai báo biến chỉ được nâng lên

Tuy nhiên,

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
0 là
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var varialbe1 = variable2 = varialbe3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // 1. Aggh!
1 có nghĩa là
function test() {
 	var [a, b, c, d] = Array(4).fill(1);
 	console.log(a,b,c,d) //1, 1, 1, 1
}
test();
console.log(window.a); // undefined
1

Điều đó có nghĩa là variable3 sẽ được gán cho 1 trước, sau đó giá trị của variable3 sẽ được gán cho variable2, và cuối cùng, giá trị của variable2 sẽ được gán cho variable1

Để tránh rò rỉ biến trong

function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
2, chúng ta có thể tách phần khai báo và gán biến thành hai dòng riêng biệt. Bằng cách này, chúng tôi có thể giới hạn phạm vi chức năng của variable1, variable2variable3 đối với
function test1() {
 	var variable1 = 1, variable2 = 1, varialbe3 = 1;
}

function test2() {
 	var variable1, variable2, variable3;
 	variable1 = variable2 = variable3 = 1;
}

test1();
console.log(window.variable2); // undefined

test2();
console.log(window.variable2); // undefined
2

________số 8_______

đầu ra

Phép gán nhiều biến bằng cách sử dụng phép gán hủy cấu trúc với hàm =3 trong JavaScript

function test() {
 	var [a, b, c, d] = Array(4).fill(1);
 	console.log(a,b,c,d) //1, 1, 1, 1
}
test();
console.log(window.a); // undefined

đầu ra

Phép gán phá hủy giúp gán nhiều biến có cùng giá trị mà không rò rỉ chúng ra bên ngoài hàm

Phương thức =3 cập nhật tất cả các phần tử mảng với giá trị tĩnh và trả về mảng đã sửa đổi. Bạn có thể đọc thêm về =3 tại đây