Destructuring assignment parameter
Trong JavaScript, cú pháp destructuring assignment cũng có thể được sử dụng cho tham số của function. Khi tham số là object hoặc array, destructuring assignment parameter rất tiện lợi nếu bạn chỉ muốn sử dụng một phần của object hoặc array đó trong function.
Cú pháp destructuring assignment parameter
Trong JavaScript, cú pháp destructuring assignment parameter cho object được viết bằng cách đặt tên property của object trong ngoặc nhọn. Tên tham số phải trùng với tên property.
jsfunctionfoo ({a ,b }) {console .log (a ,b );}foo ({a : 1,b : 2,c : 3 });
jsfunctionfoo ({a ,b }) {console .log (a ,b );}foo ({a : 1,b : 2,c : 3 });
Để nhận property với tên tham số khác, bạn chỉ định tên tham số sau dấu :.
jsfunctionfoo ({a :x ,b :y }) {console .log (x ,y );}foo ({a : 1,b : 2 });
jsfunctionfoo ({a :x ,b :y }) {console .log (x ,y );}foo ({a : 1,b : 2 });
Destructuring assignment parameter cho array được viết bằng cách đặt tên biến sẽ được gán phần tử array trong ngoặc vuông. Bạn có thể tự do chọn tên tham số tương ứng với phần tử array.
jsfunctionbar ([a ,b ]) {console .log (a ,b );}bar ([1, 2, 3]);
jsfunctionbar ([a ,b ]) {console .log (a ,b );}bar ([1, 2, 3]);
Destructuring assignment parameter cũng có thể được sử dụng với arrow function.
jsconstfoo = ({a ,b }) => {};constbar = ([a ,b ]) => {};
jsconstfoo = ({a ,b }) => {};constbar = ([a ,b ]) => {};
Type annotation cho destructuring assignment parameter
Trong TypeScript, khi destructure object, type annotation cho object được viết bên phải destructuring assignment parameter.
tsfunctionfoo ({a ,b }: {a : number;b : number }) {}// ^^^^^^^^^^^^^^^^^^^^^^^^^^Type annotation
tsfunctionfoo ({a ,b }: {a : number;b : number }) {}// ^^^^^^^^^^^^^^^^^^^^^^^^^^Type annotation
Khi destructure array, type annotation cho array type cũng được viết bên phải destructuring assignment parameter.
tsfunctionbar ([num1 ]: number[]) {}
tsfunctionbar ([num1 ]: number[]) {}
Khi type annotation là array type và compiler option noUncheckedIndexedAccess được bật, destructuring assignment parameter sẽ trở thành union type với undefined.
tsfunctionbar ([num1 ]: number[]) {}
tsfunctionbar ([num1 ]: number[]) {}
📄️ noUncheckedIndexedAccess
Bắt buộc check undefined khi tham chiếu property của index type hoặc phần tử array
Nếu type annotation của destructuring assignment parameter cho array là tuple type, ngay cả khi noUncheckedIndexedAccess được bật, nó sẽ không trở thành union type với undefined.
tsfunctionbar ([num1 ,num2 ]: [number, number]) {}
tsfunctionbar ([num1 ,num2 ]: [number, number]) {}
Giá trị mặc định và lỗi compile
Trong JavaScript, nếu không có object property hoặc array element tương ứng với destructuring assignment parameter, undefined sẽ được gán.
jsfunctionfoo ({a }) {console .log (a );}functionbar ([a ]) {console .log (a );}foo ({});bar ([]);
jsfunctionfoo ({a }) {console .log (a );}functionbar ([a ]) {console .log (a );}foo ({});bar ([]);
Mặt khác, trong TypeScript, nếu không có object property hoặc array element tương ứng với destructuring assignment parameter, sẽ xảy ra lỗi compile.
tsfunctionfoo ({a }: {a : number }) {}functionbar ([a ]: [number]) {}Argument of type '{}' is not assignable to parameter of type '{ a: number; }'. Property 'a' is missing in type '{}' but required in type '{ a: number; }'.2345Argument of type '{}' is not assignable to parameter of type '{ a: number; }'. Property 'a' is missing in type '{}' but required in type '{ a: number; }'.foo ({} );Argument of type '[]' is not assignable to parameter of type '[number]'. Source has 0 element(s) but target requires 1.2345Argument of type '[]' is not assignable to parameter of type '[number]'. Source has 0 element(s) but target requires 1.bar ([] );
tsfunctionfoo ({a }: {a : number }) {}functionbar ([a ]: [number]) {}Argument of type '{}' is not assignable to parameter of type '{ a: number; }'. Property 'a' is missing in type '{}' but required in type '{ a: number; }'.2345Argument of type '{}' is not assignable to parameter of type '{ a: number; }'. Property 'a' is missing in type '{}' but required in type '{ a: number; }'.foo ({} );Argument of type '[]' is not assignable to parameter of type '[number]'. Source has 0 element(s) but target requires 1.2345Argument of type '[]' is not assignable to parameter of type '[number]'. Source has 0 element(s) but target requires 1.bar ([] );
Default parameter cho destructuring assignment parameter
Trong JavaScript, để chỉ định default parameter cho destructuring assignment parameter, bạn viết = và giá trị mặc định sau tên tham số.
jsfunctionfoo ({a = 0 }) {// ^^^Chỉ định giá trị mặc địnhconsole .log (a );}functionbar ([a = 0]) {// ^^^Chỉ định giá trị mặc địnhconsole .log (a );}foo ({});bar ([]);
jsfunctionfoo ({a = 0 }) {// ^^^Chỉ định giá trị mặc địnhconsole .log (a );}functionbar ([a = 0]) {// ^^^Chỉ định giá trị mặc địnhconsole .log (a );}foo ({});bar ([]);
Trong TypeScript, khi thêm type annotation cho default parameter, bạn đánh dấu property là optional bằng ? cho object.
tsfunctionfoo ({a = 0 }: {a ?: number | string }) {}
tsfunctionfoo ({a = 0 }: {a ?: number | string }) {}
Nếu kiểu của property có thể được suy luận từ giá trị mặc định, type annotation có thể được bỏ qua.
tsfunctionfoo ({a = 0 }) {}
tsfunctionfoo ({a = 0 }) {}
Giá trị mặc định cho toàn bộ destructuring assignment parameter
Để chỉ định giá trị mặc định cho toàn bộ destructuring assignment parameter, bạn viết = và giá trị mặc định sau cú pháp destructuring assignment. Giá trị mặc định này được sử dụng khi toàn bộ tham số không có hoặc là undefined.
jsfunctionfoo ({a ,b } = {a : 0,b : 0 }) {console .log (a ,b );}foo ();foo ({a : 1 });functionbar ([a ,b ] = [0, 0]) {console .log (a ,b );}bar ();bar ([1]);
jsfunctionfoo ({a ,b } = {a : 0,b : 0 }) {console .log (a ,b );}foo ();foo ({a : 1 });functionbar ([a ,b ] = [0, 0]) {console .log (a ,b );}bar ();bar ([1]);
Trong TypeScript, giá trị mặc định cho toàn bộ tham số được viết sau type annotation.
ts// ................Vị trí type annotationfunctionfoo ({a }: {a ?: number } = {a : 0 }) {}// ^^^^^^^^^^Vị trí giá trị mặc định
ts// ................Vị trí type annotationfunctionfoo ({a }: {a ?: number } = {a : 0 }) {}// ^^^^^^^^^^Vị trí giá trị mặc định
Bạn cũng có thể chỉ định cả giá trị mặc định cho từng property và giá trị mặc định cho toàn bộ tham số. Trong trường hợp này, khi bỏ qua toàn bộ tham số, giá trị mặc định của từng property sẽ được sử dụng.
tstypeObj = {a ?: number;b ?: number };functionfoo ({a = 0,b = 0 }:Obj = {}) {console .log (a +b );}foo ();foo ({});foo ({a : 1 });foo ({a : 1,b : 2 });
tstypeObj = {a ?: number;b ?: number };functionfoo ({a = 0,b = 0 }:Obj = {}) {console .log (a +b );}foo ();foo ({});foo ({a : 1 });foo ({a : 1,b : 2 });
Bỏ qua tên property khi gọi
Trong JavaScript, nếu biến có cùng tên với destructuring assignment parameter đã được định nghĩa, bạn có thể bỏ qua tên property trong object literal và chỉ truyền biến.
tsfunctionbmi ({height ,weight }: {height : number;weight : number }) {}// Biến có cùng tên với propertyconstheight = 170;constweight = 65;// Gọi không bỏ qua tên propertybmi ({height :height ,weight :weight });// Gọi bỏ qua tên propertybmi ({weight ,height });
tsfunctionbmi ({height ,weight }: {height : number;weight : number }) {}// Biến có cùng tên với propertyconstheight = 170;constweight = 65;// Gọi không bỏ qua tên propertybmi ({height :height ,weight :weight });// Gọi bỏ qua tên propertybmi ({weight ,height });
📄️ Shorthand property names
Khi key của object và tên biến giống nhau, có thể sử dụng shorthand property names khi gán giá trị vào object. Đây là tính năng liên quan đến destructuring assignment. Ví dụ sau minh họa gần như toàn bộ.
Chia sẻ kiến thức
・Destructuring assignment parameter tiện lợi khi sử dụng một phần của object hoặc array trong function
・Đối với object, viết tham số trong ngoặc nhọn
→ function foo({ a, b })
・Đối với array, viết tham số trong ngoặc vuông
→ function foo([a, b])
・Type annotation được viết sau destructuring assignment
・Cũng có thể chỉ định giá trị mặc định
→ function foo({ a = 0})
Từ 『Survival TypeScript』
Thông tin liên quan
📄️ Destructuring assignment của object
JavaScript có cú pháp tiện lợi là destructuring assignment để lấy property từ object.
📄️ Destructuring assignment của array
Cơ bản