Các đối tượng của javascript là gì?
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à trong khi 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", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }0 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3____14 let school = { "school name" : "Vivekananda School", }5 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3 let school = { "school name" : "Vivekananda School", }8____19 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3____122 let school = { "school name" : "Vivekananda School", }23 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3____126 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }28 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }00 let school = { "school name" : "Vivekananda School", }01 let school = { "school name" : "Vivekananda School", }02 let school = { "school name" : "Vivekananda School", }03 let school = { "school name" : "Vivekananda School", }3____205 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }09 let school = { "school name" : "Vivekananda School", }80 Đầu ra.
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 của đố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. javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3____14 let school = { "school name" : "Vivekananda School", }5 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3 let school = { "school name" : "Vivekananda School", }8____19 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3____122 let school = { "school name" : "Vivekananda School", }23 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3____437 let school = { "school name" : "Vivekananda School", }3____126 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }28 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }93____494 let school = { "school name" : "Vivekananda School", }95 let school = { "school name" : "Vivekananda School", }3____205 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }09 let school = { "school name" : "Vivekananda School", }80 Đầu ra. 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. javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }85 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3____14 let school = { "school name" : "Vivekananda School", }5 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }3____126 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }28 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }337____2338 let school = { "school name" : "Vivekananda School", }339 let school = { "school name" : "Vivekananda School", }3____205 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 ________ 234 ________ 2345 ________ 2346 let school = { "school name" : "Vivekananda School", }80 Đầu ra. 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
javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }00 ________ 234 ________ 102 ________ 103 ________ 104 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }06 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }09 let school = { "school name" : "Vivekananda School", }10 let school = { "school name" : "Vivekananda School", }11 let school = { "school name" : "Vivekananda School", }12 let school = { "school name" : "Vivekananda School", }80 đầu ra. Tạo đối tượng Có một số cách hoặc cú pháp để tạo đối tượng. Một trong số đó, được gọi là cú pháp chữ Đối tượng, chúng tôi đã sử dụng. Bên cạnh cú pháp theo nghĩa đen của đối tượng, các đối tượng trong JavaScript cũng có thể được tạo bằng cách sử dụng hàm tạo, Trình tạo đối tượng hoặc mẫu nguyên mẫu Sử dụng cú pháp đối tượng bằng chữ. Cú pháp nghĩa đen của đối tượng sử dụng ký hiệu {…} để khởi tạo trực tiếp một đối tượng và các phương thức/thuộc tính của nó. Chúng ta hãy xem một ví dụ về việc tạo các đối tượng bằng phương pháp này let school = { "school name" : "Vivekananda School", }2 Các thành viên này có thể là bất kỳ thứ gì – chuỗi, số, hàm, mảng hoặc thậm chí các đối tượng khác. Một đối tượng như thế này được gọi là một đối tượng theo nghĩa đen. Điều này khác với các phương pháp tạo đối tượng khác liên quan đến việc sử dụng hàm tạo và lớp hoặc nguyên mẫu, đã được thảo luận bên dưới Trình tạo đối tượng. Một cách khác để tạo đối tượng trong JavaScript là sử dụng hàm tạo "Đối tượng". Trình tạo đối tượng tạo một trình bao bọc đối tượng cho giá trị đã cho. Điều này, được sử dụng cùng với từ khóa “mới” cho phép chúng tôi khởi tạo các đối tượng mới Thí dụ javlet school = { "school name" : "Vivekananda School", }33 ________ 234 ________ 116 ________ 103 ________ 104 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }20 let school = { "school name" : "Vivekananda School", }21 let school = { "school name" : "Vivekananda School", }22 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }24____19 let school = { "school name" : "Vivekananda School", }22 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }28 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }31 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }28 let school = { "school name" : "Vivekananda School", }3____200 ________ 136 ________ 202 ________ 203 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }345 let school = { "school name" : "Vivekananda School", }80 Đầu ra. nhà xây dựng. Trình tạo trong JavaScript, giống như trong hầu hết các ngôn ngữ OOP khác, cung cấp mẫu để tạo đối tượng. Nói cách khác, nó định nghĩa một tập hợp các thuộc tính và phương thức chung cho tất cả các đối tượng được khởi tạo bằng cách sử dụng hàm tạo. Hãy để chúng tôi xem một ví dụ. javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }48 let school = { "school name" : "Vivekananda School", }49 let school = { "school name" : "Vivekananda School", }50 let school = { "school name" : "Vivekananda School", }51 let school = { "school name" : "Vivekananda School", }49 let school = { "school name" : "Vivekananda School", }50____154 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }59 let school = { "school name" : "Vivekananda School", }03 let school = { "school name" : "Vivekananda School", }61 let school = { "school name" : "Vivekananda School", }62 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }64 let school = { "school name" : "Vivekananda School", }65 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }67 let school = { "school name" : "Vivekananda School", }03 let school = { "school name" : "Vivekananda School", }61 let school = { "school name" : "Vivekananda School", }70 let school = { "school name" : "Vivekananda School", }6 let school = { "school name" : "Vivekananda School", }72 let school = { "school name" : "Vivekananda School", }73 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }76 let school = { "school name" : "Vivekananda School", }77 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }79 let school = { "school name" : "Vivekananda School", }80 let school = { "school name" : "Vivekananda School", }80 Đầu ra.
nguyên mẫu. Một cách khác để tạo các đối tượng liên quan đến việc sử dụng nguyên mẫu. Theo mặc định, mọi hàm JavaScript đều có thuộc tính đối tượng nguyên mẫu (theo mặc định, nó trống). Các phương thức hoặc thuộc tính có thể được gắn vào thuộc tính này. Mô tả chi tiết về nguyên mẫu nằm ngoài phạm vi giới thiệu về đối tượng này. Tuy nhiên, bạn có thể tự làm quen với cú pháp cơ bản được sử dụng như bên dưới let school = { "school name" : "Vivekananda School", }0 Một ví dụ về việc sử dụng Object. phương thức tạo () là. javlet school = { "school name" : "Vivekananda School", }80 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }84 let school = { "school name" : "Vivekananda School", }3____186 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }89 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }91 let school = { "school name" : "Vivekananda School", }89 let school = { "school name" : "Vivekananda School", }3____194 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }96 let school = { "school name" : "Vivekananda School", }80 Đầu ra. 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", }8 javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3____1202 let school = { "school name" : "Vivekananda School", }3____1204 let school = { "school name" : "Vivekananda School", }3____1206 let school = { "school name" : "Vivekananda School", }3____437 let school = { "school name" : "Vivekananda School", }3____1210 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }212 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }00 ________ 136 ________ 202 ________ 203 let school = { "school name" : "Vivekananda School", }3____205 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }224 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }227 let school = { "school name" : "Vivekananda School", }80 Đầu ra. Ký hiệu dấu ngoặc. let school = { "school name" : "Vivekananda School", }3 javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }2 let school = { "school name" : "Vivekananda School", }3____1233 let school = { "school name" : "Vivekananda School", }3____1204 let school = { "school name" : "Vivekananda School", }3____1237 let school = { "school name" : "Vivekananda School", }3____437 let school = { "school name" : "Vivekananda School", }3____1210 let school = { "school name" : "Vivekananda School", }27 let school = { "school name" : "Vivekananda School", }212 let school = { "school name" : "Vivekananda School", }29 let school = { "school name" : "Vivekananda School", }245 ________ 136 ________ 202 ________ 203 let school = { "school name" : "Vivekananda School", }3____205 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }255 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }257____1258 let school = { "school name" : "Vivekananda School", }259 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34____1262 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }257____494 let school = { "school name" : "Vivekananda School", }259 let school = { "school name" : "Vivekananda School", }80 đầ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", }9 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", }8 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 Ví dụ. javlet school = { "school name" : "Vivekananda School", }33 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }270 let school = { "school name" : "Vivekananda School", }3____1272 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }05 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }277 let school = { "school name" : "Vivekananda School", }278 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }280 _______234____1282 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }284 let school = { "school name" : "Vivekananda School", }07 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }287 let school = { "school name" : "Vivekananda School", }288 let school = { "school name" : "Vivekananda School", }02 let school = { "school name" : "Vivekananda School", }290 let school = { "school name" : "Vivekananda School", }34____1292 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }294 let school = { "school name" : "Vivekananda School", }3____1296 let school = { "school name" : "Vivekananda School", }34 let school = { "school name" : "Vivekananda School", }298 let school = { "school name" : "Vivekananda School", }80 đầ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. Tất cả các đối tượng trong JavaScript là gì?Đối tượng JavaScript . Booleans có thể là đối tượng (nếu được xác định bằng từ khóa mới) Số có thể là đối tượng (nếu được xác định bằng từ khóa mới) Chuỗi có thể là đối tượng (nếu được xác định bằng từ khóa mới) Ngày luôn là đối tượng Toán học luôn là đối tượng Biểu thức chính quy luôn là đối tượng Mảng luôn là đối tượng Đối tượng JavaScript trong Java là gì?Trong JavaScript có các loại dữ liệu "đối tượng" trong đó một biến nhất định về cơ bản có các biến phụ với các giá trị duy nhất của riêng chúng , chẳng hạn. var xe = {loại. "Fiat", mô hình. 500, màu. "trắng"}; .
Đối tượng và lớp trong JavaScript là gì?Lớp là khuôn mẫu để tạo đối tượng . Họ đóng gói dữ liệu bằng mã để làm việc trên dữ liệu đó. Các lớp trong JS được xây dựng trên nguyên mẫu nhưng cũng có một số cú pháp và ngữ nghĩa không được chia sẻ với ngữ nghĩa giống như lớp ES5.
Tại sao các đối tượng được sử dụng trong JavaScript?Các đối tượng trong lập trình có thể là sự kết hợp của các biến, hàm và cấu trúc dữ liệu. Điều này có nghĩa là các đối tượng có thể lưu trữ các giá trị, bạn có thể sử dụng các đối tượng để thao tác các giá trị và kết hợp chúng thành các đối tượng phức tạp hơn , chẳng hạn như mảng mà vẫn nhận được tất cả các lợi ích. |