Cú pháp tham số REST cho phép một hàm chấp nhận số lượng đối số không xác định như một mảng, cung cấp một cách để biểu diễn các hàm variadic trong JavaScript.rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.Thử nó
Cú pháp
function f[a, b, ...theArgs] {
// …
}
Sự mô tả
Tham số cuối cùng của một hàm có thể được đặt trước 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"]
4 [ba ký tự dừng hoàn toàn U+002E], 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 mảng JavaScript tiêu chuẩn. Chỉ tham số cuối cùng trong một định nghĩa hàm có thể là một tham số REST.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"]
Tham khảo nhanh
Một định nghĩa hàm chỉ có thể có một
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"]
4RestParam.foo[...one, ...wrong, ...wrong]
Tham số REST phải là tham số cuối cùng trong định nghĩa hàm.
foo[...wrong, arg2, arg3]
foo[arg1, arg2, ...correct]
Sự khác biệt giữa các tham số REST và đối tượng đối số
Có ba khác biệt chính giữa các tham số REST 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"]
6:- Đối tượng
6 không phải là một mảng thực, trong khi các tham số REST là các trường hợpfunction 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, 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"]
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,foo[...one, ...wrong, ...wrong]
1 hoặcfoo[...one, ...wrong, ...wrong]
2 có thể được áp dụng trực tiếp trên nó.not a real array, while rest parameters arefoo[...one, ...wrong, ...wrong]
8 instances, meaning methods likefunction 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,foo[...one, ...wrong, ...wrong]
1 orfoo[...one, ...wrong, ...wrong]
2 can be applied on it directly.foo[...one, ...wrong, ...wrong]
- Đối tượng
6 có chức năng bổ sung cụ thể cho chính nó [như thuộc tí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"]
4].foo[...one, ...wrong, ...wrong]
- Các gói
5 tất cả các tham số bổ sung vào một mảng, do đó nó không chứa bất kỳ đối số được đặt tên nào được xác định trướcfoo[...one, ...wrong, ...wrong]
5. Trong khi đối tượngfoo[...one, ...wrong, ...wrong]
6 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ói thành một đối tượng giống như một mảng.before thefoo[...one, ...wrong, ...wrong]
5. Whereas thefoo[...one, ...wrong, ...wrong]
6 object contains all of the parameters — including the parameters in thefunction 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 array — bundled into one array-like object.foo[...one, ...wrong, ...wrong]
Từ đối số đến một mảng
Các tham số REST đã được giới thiệu để giảm mã nồi hơi 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 khi các tham số REST,
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"]
6 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:function fn[a, b] {
const normalArray = Array.prototype.slice.call[arguments];
// — or —
const normalArray2 = [].slice.call[arguments];
// — or —
const normalArrayFrom = Array.from[arguments];
const first = normalArray.shift[]; // OK, gives the first argument
const firstBad = arguments.shift[]; // ERROR [arguments is not a normal array]
}
Bây giờ, bạn có thể dễ dàng truy cập vào một mảng bình thường bằng tham số REST:
function fn[...args] {
const normalArray = args;
const first = normalArray.shift[]; // OK, gives the first argument
}
Ví dụ
Sử dụng các tham số REST
Trong ví dụ này, đối số đầu tiên được ánh xạ tới
foo[...wrong, arg2, arg3]
0 và thứ hai đến foo[...wrong, arg2, arg3]
1, vì vậy các đối số được đặt tên này được sử dụng là bình thường.Tuy nhiên, đối số thứ ba,
foo[...wrong, arg2, arg3]
2, sẽ là một mảng có chứa thứ ba, thứ tư, thứ năm, thứ sáu, thứ hai, thứ n - như nhiều đối số mà người dùng bao gồm.function myFun[a, b, ...manyMoreArgs] {
console.log["a", a];
console.log["b", b];
console.log["manyMoreArgs", manyMoreArgs];
}
myFun["one", "two", "three", "four", "five", "six"];
// a, "one"
// b, "two"
// manyMoreArgs, ["three", "four", "five", "six"]