Class là object
Đặc điểm của class trong JavaScript là class cũng là một loại object. Object trong JavaScript là tập hợp các property. Vì class cũng là object nên có tính chất là tập hợp các property. Do đó, có thể thêm hoặc thay đổi property của class đã định nghĩa.
jsconstmyObject = {};myObject .key = "value"; // Thêm propertyclassMyClass {}MyClass .key = "value"; // Thêm property
jsconstmyObject = {};myObject .key = "value"; // Thêm propertyclassMyClass {}MyClass .key = "value"; // Thêm property
Ngược lại, trong TypeScript không cho phép mở rộng động như vậy để đảm bảo type-safe.
tsclassMyClass {}Property 'key' does not exist on type 'typeof MyClass'.2339Property 'key' does not exist on type 'typeof MyClass'.MyClass .= "value"; key
tsclassMyClass {}Property 'key' does not exist on type 'typeof MyClass'.2339Property 'key' does not exist on type 'typeof MyClass'.MyClass .= "value"; key