Các đối tượng, trong JavaScript, là loại dữ liệu quan trọng nhất và tạo thành các khối xây dựng cho JavaScript hiện đại. Các đối tượng này hoàn toàn khác với các kiểu dữ liệu nguyên thủy của JavaScript (Số, Chuỗi, Boolean, null, không xác định và ký hiệu) theo nghĩa là tất cả các kiểu dữ liệu nguyên thủy này đều lưu trữ một giá trị duy nhất (tùy thuộc vào loại của chúng) Show cú pháp let object_name = { key_name : value, ... } Chúng ta hãy xem một ví dụ về Đối tượng JavaScript bên dưới javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }60 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }0 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3 let school = { "school name" : "Vivekananda School", }4 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }6____17 let school = { "school name" : "Vivekananda School", }4 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }11 let school = { "school name" : "Vivekananda School", }4 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }14 let school = { "school name" : "Vivekananda School", }15 let school = { "school name" : "Vivekananda School", }16 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }18 let school = { "school name" : "Vivekananda School", }19 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }11 let school = { "school name" : "Vivekananda School", }1____213 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }13 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }18 Đầu ra. Trong ví dụ này, “tên”, “địa điểm” và “thành lập” đều là “khóa” và “Trường Vivekananda”, “Delhi” và 1971 lần lượt là giá trị của các khóa này. Mỗi khóa này được gọi là thuộc tính của đối tượng. Một đối tượng trong JavaScript cũng có thể có một chức năng như một thành viên, trong trường hợp đó, nó sẽ được gọi là một phương thức của đối tượng đó. Ở đây “displayinfo” là một phương thức của đối tượng trường học đang được sử dụng để làm việc với dữ liệu của đối tượng, được lưu trữ trong các thuộc tính của nó.
Một đối tượng có thể được tạo bằng dấu ngoặc nhọn {…} với danh sách thuộc tính tùy chọn. Thuộc tính là “chìa khóa. cặp giá trị”, trong đó khóa là một chuỗi (còn được gọi là “tên thuộc tính”) và giá trị có thể là bất kỳ thứ gì Thuộc tính đối tượng JavaScript. Tên thuộc tính có thể là chuỗi hoặc số. Trong trường hợp tên thuộc tính là số, chúng phải được truy cập bằng cách sử dụng "ký hiệu ngoặc" như thế này Thí dụ javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }0 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3 let school = { "school name" : "Vivekananda School", }4 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }6____17 let school = { "school name" : "Vivekananda School", }4 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }11 let school = { "school name" : "Vivekananda School", }4 _______11____425 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }14 let school = { "school name" : "Vivekananda School", }15 let school = { "school name" : "Vivekananda School", }16 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }581______1582 let school = { "school name" : "Vivekananda School", }583 let school = { "school name" : "Vivekananda School", }1____213 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }13 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }18 Đầu ra. Nhưng nhiều hơn về ký hiệu dấu ngoặc sau. Tên thuộc tính cũng có thể là các chuỗi có nhiều từ được phân tách bằng dấu cách. Trong trường hợp đó, các tên thuộc tính này phải được đặt trong dấu ngoặc kép.let school = { "school name" : "Vivekananda School", } Giống như tên thuộc tính là số, chúng cũng phải được truy cập bằng cách sử dụng ký hiệu dấu ngoặc. Giống như nếu chúng ta muốn truy cập 'Vivekananda' từ 'Vivekananda School', chúng ta có thể làm điều gì đó như thế này Thí dụ javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }593 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }0 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3 let school = { "school name" : "Vivekananda School", }4 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }14 let school = { "school name" : "Vivekananda School", }15 let school = { "school name" : "Vivekananda School", }16 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }605____1606 let school = { "school name" : "Vivekananda School", }607 let school = { "school name" : "Vivekananda School", }1____213 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }13 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }593 let school = { "school name" : "Vivekananda School", }594 let school = { "school name" : "Vivekananda School", }18 đầu ra. Trong đoạn mã trên, chúng tôi đã sử dụng ký hiệu ngoặc và cả phương thức phân tách do JavaScript cung cấp mà bạn sẽ tìm hiểu trong bài viết về chuỗi Thuộc tính kế thừa. Thuộc tính kế thừa của một đối tượng là những thuộc tính đã được kế thừa từ nguyên mẫu của đối tượng, trái ngược với việc được xác định cho chính đối tượng, được gọi là thuộc tính riêng của đối tượng. Để xác minh xem một thuộc tính có phải là thuộc tính Riêng của đối tượng hay không, chúng ta có thể sử dụng phương thức hasOwnProperty. Thuộc tính thuộc tính Thuộc tính dữ liệu trong JavaScript có bốn thuộc tính
Thí dụ javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }598 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }00____101 let school = { "school name" : "Vivekananda School", }02 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }04 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }07____108 let school = { "school name" : "Vivekananda School", }09 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }18 đầu ra. Tạo đối tượng. Để tạo đối tượng tham khảo bài viết sau. Giới thiệu. - https. //www. chuyên viên máy tính. org/?p=190694 Truy cập thành viên đối tượng. Thành viên đối tượng (thuộc tính hoặc phương pháp) có thể được truy cập bằng cách sử dụng ký hiệu dấu chấm let school = { "school name" : "Vivekananda School", }1 Thí dụ javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }0 let school = { "school name" : "Vivekananda School", }1____116 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }18 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }20 _______11____425 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }24 let school = { "school name" : "Vivekananda School", }15 let school = { "school name" : "Vivekananda School", }26 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }18 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }11 let school = { "school name" : "Vivekananda School", }1____213 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }13 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }38 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }41 let school = { "school name" : "Vivekananda School", }18 đầu ra. Ký hiệu khung let school = { "school name" : "Vivekananda School", }1 Thí dụ javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }0 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }47 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }18 let school = { "school name" : "Vivekananda School", }1____151 _______11____425 let school = { "school name" : "Vivekananda School", }1 let school = { "school name" : "Vivekananda School", }24 let school = { "school name" : "Vivekananda School", }15 let school = { "school name" : "Vivekananda School", }26 let school = { "school name" : "Vivekananda School", }17 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }11 let school = { "school name" : "Vivekananda School", }1____213 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }13 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }69 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }71____172 let school = { "school name" : "Vivekananda School", }73 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }76 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }71____1582 let school = { "school name" : "Vivekananda School", }73 let school = { "school name" : "Vivekananda School", }18 đầu ra. Không giống như ký hiệu dấu chấm, từ khóa dấu ngoặc hoạt động với bất kỳ tổ hợp chuỗi nào, bao gồm nhưng không giới hạn đối với chuỗi nhiều từ. Ví dụ let school = { "school name" : "Vivekananda School", }32 Không giống như ký hiệu dấu chấm, ký hiệu dấu ngoặc cũng có thể chứa các tên là kết quả của bất kỳ biến biểu thức nào có giá trị được tính trong thời gian chạy. Ví dụ let school = { "school name" : "Vivekananda School", }2 Các thao tác tương tự không thể thực hiện được khi sử dụng ký hiệu dấu chấm Lặp lại tất cả các khóa của một đối tượng. Để lặp lại tất cả các khóa có thể đếm được hiện có của một đối tượng, chúng ta có thể sử dụng cấu trúc for…in. Điều đáng chú ý là điều này cho phép chúng ta chỉ truy cập các thuộc tính của một đối tượng có thể đếm được (Hãy nhớ rằng có thể đếm được là một trong bốn thuộc tính của thuộc tính dữ liệu). Chẳng hạn, các thuộc tính được kế thừa từ Đối tượng. nguyên mẫu không đếm được. Tuy nhiên, vô số thuộc tính được kế thừa từ đâu đó cũng có thể được truy cập bằng cách sử dụng cấu trúc for…in Thí dụ. javlet school = { "school name" : "Vivekananda School", }58 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }84 let school = { "school name" : "Vivekananda School", }1____186____187 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }13 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }92 let school = { "school name" : "Vivekananda School", }93 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }95______196 let school = { "school name" : "Vivekananda School", }97 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }99 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }101____1102 let school = { "school name" : "Vivekananda School", }97 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }106 let school = { "school name" : "Vivekananda School", }107 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }109 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }111 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }113 let school = { "school name" : "Vivekananda School", }1____1115 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }117 let school = { "school name" : "Vivekananda School", }18 đầu ra. Xóa thuộc tính. Để xóa một thuộc tính của một đối tượng, chúng ta có thể sử dụng toán tử xóa. Một ví dụ về việc sử dụng nó đã được liệt kê dưới đây Khi nào tôi nên sử dụng các đối tượng trong JavaScript?Như chúng ta đã biết từ chương Kiểu dữ liệu, có tám kiểu dữ liệu trong JavaScript. Bảy trong số chúng được gọi là "nguyên thủy", bởi vì giá trị của chúng chỉ chứa một thứ duy nhất (có thể là một chuỗi hoặc một số hoặc bất cứ thứ gì). Ngược lại, các đối tượng được sử dụng để lưu trữ các bộ sưu tập có khóa gồm nhiều dữ liệu khác nhau và các thực thể phức tạp hơn .
Đối tượng đối tượng có nghĩa là gì trong JavaScript?[đối tượng, đối tượng] là biểu diễn chuỗi của loại dữ liệu đối tượng JavaScript . Bạn sẽ hiểu rõ hơn khi chúng ta đi xa hơn trong bài viết này. Có hai bối cảnh chính mà bạn sẽ gặp phải kết quả như vậy. Khi bạn thử hiển thị một đối tượng bằng phương thức alert() (phổ biến nhất).
Loại đối tượng trong JavaScript là gì?Một loại đối tượng chỉ đơn giản là tập hợp các thuộc tính ở dạng cặp tên và giá trị . Lưu ý từ danh sách rằng null và undefined là các kiểu dữ liệu JavaScript nguyên thủy, mỗi kiểu là một kiểu dữ liệu chỉ chứa một giá trị. |