@@ -6,7 +6,7 @@ Original Repository: [ryanmcdermott/clean-code-javascript](https://github.com/ry
66 1 . [ Introduction] ( #introduction )
77 2 . [ Biến] ( #biến )
88 3 . [ Functions] ( #functions )
9- 4 . [ Objects and Data Structures ] ( #objects-and-data-structures )
9+ 4 . [ Đối tượng và Cấu trúc dữ liệu ] ( #đối-tượng-và-cấu-trúc-dữ-liệu )
1010 5 . [ Classes] ( #classes )
1111 6 . [ Testing] ( #testing )
1212 7 . [ Concurrency] ( #concurrency )
@@ -923,25 +923,24 @@ inventoryTracker('apples', req, 'www.inventory-awesome.io');
923923```
924924** [ ⬆ back to top] ( #table-of-contents ) **
925925
926- ## ** Objects and Data Structures **
927- ### Use getters and setters
928- JavaScript doesn't have interfaces or types so it is very hard to enforce this
929- pattern, because we don't have keywords like ` public ` and ` private ` . As it is,
930- using getters and setters to access data on objects is far better than simply
931- looking for a property on an object. "Why?" you might ask. Well, here's an
932- unorganized list of reasons why :
926+ ## ** Đối tượng và Cấu trúc dữ liệu **
927+ ### Sử dụng getter và setter
928+ JavaScript không có interface hoặc kiểu vì vậy rất khó để thực hiện mô hình này,
929+ bởi vì chúng ta không có các từ khoá như ` public ` và ` private ` . Vì vậy, sử dụng
930+ getters và setters để truy cập dữ liệu trên các đối tượng thì tốt hơn là chỉ đơn
931+ giản tìm kiếm một thuộc tính trên một đối tượng. Bạn có thể hỏi "Tại sao?".
932+ Đây là một danh sách các lí do tại sao :
933933
934- * When you want to do more beyond getting an object property, you don't have
935- to look up and change every accessor in your codebase.
936- * Makes adding validation simple when doing a ` set ` .
937- * Encapsulates the internal representation.
938- * Easy to add logging and error handling when getting and setting.
939- * Inheriting this class, you can override default functionality.
940- * You can lazy load your object's properties, let's say getting it from a
941- server.
934+ * Khi bạn muốn thực hiện nhiều hơn việc lấy một thuộc tính của đối tượng, bạn không
935+ cần phải tìm kiếm và thay đổi mỗi accessor trong codebase của bạn.
936+ * Làm cho việc thêm các validation đơn giản khi thực hiện trên một ` tập hợp ` .
937+ * Đóng gói các biểu diễn nội bộ.
938+ * Dễ dàng thêm log và xử lí lỗi khi getting và setting.
939+ * Kế thừa lớp này, bạn có thể override những hàm mặc định.
940+ * Bạn có thể lazy load các thuộc tính của một đối tượng, lấy nó từ server.
942941
943942
944- ** Bad :**
943+ ** Không tốt :**
945944``` javascript
946945class BankAccount {
947946 constructor () {
@@ -955,14 +954,14 @@ const bankAccount = new BankAccount();
955954bankAccount .balance -= 100 ;
956955```
957956
958- ** Good :**
957+ ** Tốt :**
959958``` javascript
960959class BankAccount {
961960 constructor (balance = 1000 ) {
962961 this ._balance = balance;
963962 }
964963
965- // It doesn't have to be prefixed with `get` or `set` to be a getter/ setter
964+ // Không cần phải thêm tiền tố `get` hay `set` để trở thành một getter hay setter
966965 set balance (amount ) {
967966 if (this .verifyIfAmountCanBeSetted (amount)) {
968967 this ._balance = amount;
@@ -987,13 +986,13 @@ bankAccount.balance -= shoesPrice;
987986let balance = bankAccount .balance ;
988987
989988```
990- ** [ ⬆ back to top ] ( #table-of-contents ) **
989+ ** [ ⬆ về đầu trang ] ( #table-of-contents ) **
991990
992991
993- ### Make objects have private members
994- This can be accomplished through closures (for ES5 and below ).
992+ ### Làm cho các đối tượng có thành viên private
993+ Điều này có thể được thực hiện thông qua closures (cho ES5 và cũ hơn ).
995994
996- ** Bad :**
995+ ** Không tốt :**
997996``` javascript
998997
999998const Employee = function (name ) {
@@ -1010,7 +1009,7 @@ delete employee.name;
10101009console .log (` Employee name: ${ employee .getName ()} ` ); // Employee name: undefined
10111010```
10121011
1013- ** Good :**
1012+ ** Tốt :**
10141013``` javascript
10151014const Employee = function (name ) {
10161015 this .getName = function getName () {
@@ -1023,7 +1022,7 @@ console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
10231022delete employee .name ;
10241023console .log (` Employee name: ${ employee .getName ()} ` ); // Employee name: John Doe
10251024```
1026- ** [ ⬆ back to top ] ( #table-of-contents ) **
1025+ ** [ ⬆ về đầu trang ] ( #table-of-contents ) **
10271026
10281027
10291028## ** Classes**
0 commit comments