Cú pháp tham số còn lại cho phép một hàm chấp nhận số lượng đối số không xác định dưới dạng một mảng, cung cấp cách biểu diễn các hàm biến thiên trong JavaScript
Thử nó
cú pháp
function f[a, b, ...theArgs] {
// …
}
Sự miêu tả
Tham số cuối cùng của định nghĩa hàm có thể được bắt đầu bằng
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
2 [ba ký tự U+002E FULL STOP], điều này sẽ khiến tất cả các tham số còn lại [do người dùng cung cấp] được đặt trong một đối tượng function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
3function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
Một định nghĩa hàm chỉ có thể có một tham số còn lại và tham số còn lại phải là tham số cuối cùng trong định nghĩa hàm
function wrong1[...one, ...wrong] {}
function wrong2[...wrong, arg2, arg3] {}
Tham số còn lại không được tính vào thuộc tính
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
4 của hàmSự khác biệt giữa các tham số còn lại và đối tượng đối số
Có ba điểm khác biệt chính giữa các tham số còn lại và đối tượng
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
5- Đối tượng
5 không phải là một mảng thực, trong khi các tham số còn lại là các thể hiện củafunction myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
3, có nghĩa là các phương thức nhưfunction myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
8,function myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
9,function myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
0 hoặcfunction wrong1[...one, ...wrong] {} function wrong2[...wrong, arg2, arg3] {}
1 có thể được áp dụng trực tiếp trên nófunction wrong1[...one, ...wrong] {} function wrong2[...wrong, arg2, arg3] {}
- Đối tượng
5 có thuộc tính bổ sung [không dùng nữa]function myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
3function wrong1[...one, ...wrong] {} function wrong2[...wrong, arg2, arg3] {}
- Trong một hàm không nghiêm ngặt với các tham số đơn giản, đối tượng
5 đồng bộ hóa các chỉ số của nó với các giá trị của tham số. Mảng tham số còn lại không bao giờ cập nhật giá trị của nó khi các tham số đã đặt tên được gán lạifunction myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
- Tham số còn lại gộp tất cả các tham số bổ sung vào một mảng, nhưng không chứa bất kỳ đối số có tên nào được xác định trước
5. Đối tượngfunction wrong1[...one, ...wrong] {} function wrong2[...wrong, arg2, arg3] {}
5 chứa tất cả các tham số — bao gồm các tham số trong mảngfunction myFun[a, b, ...manyMoreArgs] { console.log["a", a]; console.log["b", b]; console.log["manyMoreArgs", manyMoreArgs]; } myFun["one", "two", "three", "four", "five", "six"]; // Console Output: // a, one // b, two // manyMoreArgs, ["three", "four", "five", "six"]
5 — được gộp vào một đối tượng giống như mảngfunction wrong1[...one, ...wrong] {} function wrong2[...wrong, arg2, arg3] {}
ví dụ
Sử dụng tham số nghỉ ngơi
Trong ví dụ này, đối số đầu tiên được ánh xạ tới
function wrong1[...one, ...wrong] {}
function wrong2[...wrong, arg2, arg3] {}
8 và đối số thứ hai tới function wrong1[...one, ...wrong] {}
function wrong2[...wrong, arg2, arg3] {}
9, vì vậy các đối số được đặt tên này được sử dụng như bình thườngTuy nhiên, đối số thứ ba,
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
10, sẽ là một mảng chứa đối số thứ ba, thứ tư, thứ năm, thứ sáu, …, thứ n — bao nhiêu đối số mà người dùng chỉ địnhfunction myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
1Bên dưới, mặc dù chỉ có một giá trị nhưng đối số cuối cùng vẫn được đưa vào một mảng
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
3Bên dưới, đối số thứ ba không được cung cấp, nhưng
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
10 vẫn là một mảng [mặc dù là một mảng trống]function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
5Dưới đây, chỉ có một đối số được cung cấp, do đó,
function wrong1[...one, ...wrong] {}
function wrong2[...wrong, arg2, arg3] {}
9 nhận giá trị mặc định là function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
13, nhưng function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
10 vẫn là một mảng trốngfunction myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
9Độ dài đối số
Vì
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
15 là một mảng, nên số phần tử của nó được cung cấp bởi thuộc tính function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
4. Nếu tham số duy nhất của hàm là tham số còn lại, thì function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
17 sẽ bằng với function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
18function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
4Sử dụng các tham số còn lại kết hợp với các tham số thông thường
Trong ví dụ tiếp theo, một tham số còn lại được sử dụng để thu thập tất cả các tham số sau tham số đầu tiên vào một mảng. Mỗi một trong các giá trị tham số được thu thập vào mảng sau đó được nhân với tham số đầu tiên và mảng được trả về
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
5Từ đối số đến một mảng
Các phương thức
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
3 có thể được sử dụng trên các tham số còn lại, nhưng không phải trên đối tượng function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
5function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
8Các tham số còn lại đã được giới thiệu để giảm mã soạn sẵn thường được sử dụng để chuyển đổi một tập hợp các đối số thành một mảng
Trước các tham số còn lại,
function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
5 cần được chuyển đổi thành một mảng bình thường trước khi gọi các phương thức mảng trên chúng