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]
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
jav
let 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.
- Các đối tượng phức tạp hơn và mỗi đối tượng có thể chứa bất kỳ sự kết hợp nào của các kiểu dữ liệu nguyên thủy này cũng như các kiểu dữ liệu tham chiếu
- Một đối tượng là một kiểu dữ liệu tham chiếu. Các biến được gán một giá trị tham chiếu sẽ được cung cấp một tham chiếu hoặc một con trỏ tới giá trị đó. Tham chiếu hoặc con trỏ đó trỏ đến vị trí trong bộ nhớ nơi đối tượng được lưu trữ. Các biến không thực sự lưu trữ giá trị
- Nói một cách lỏng lẻo, các đối tượng trong JavaScript có thể được định nghĩa là một tập hợp dữ liệu liên quan không có thứ tự, thuộc các kiểu nguyên thủy hoặc tham chiếu, ở dạng “khóa. cặp giá trị. Các khóa này có thể là các biến hoặc hàm và được gọi tương ứng là các thuộc tính và phương thức trong ngữ cảnh của một đối tượng
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.
jav
let 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.
jav
let 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
- giá trị. Giá trị tài sản
- ghi được. Khi đúng, giá trị của thuộc tính có thể được thay đổi
- đếm được. Khi đúng, thuộc tính có thể được lặp lại bằng cách liệt kê "for-in". Mặt khác, tài sản được cho là không thể đếm được
- có thể định cấu hình. Nếu sai, các nỗ lực xóa thuộc tính, thay đổi thuộc tính thành quyền truy cập hoặc thuộc tính hoặc thay đổi thuộc tính của thuộc tính [không phải [[Giá trị]] hoặc thay đổi [[Có thể ghi]] thành sai] sẽ không thành công
jav
let 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ụ
jav
let 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ụ.
jav
let 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.
- Nó tạo một đối tượng mới và đặt thuộc tính hàm tạo của đối tượng thành trường học [Điều quan trọng cần lưu ý là thuộc tính này là thuộc tính mặc định đặc biệt không thể đếm được và không thể thay đổi bằng cách đặt “hàm tạo. someFunction” theo cách thủ công]
- Sau đó, nó thiết lập đối tượng để làm việc với đối tượng nguyên mẫu của hàm Xe [ Mỗi hàm trong JavaScript nhận một đối tượng nguyên mẫu, ban đầu chỉ là một đối tượng trống nhưng có thể được sửa đổi. Đối tượng, khi được khởi tạo, sẽ kế thừa tất cả các thuộc tính từ đối tượng nguyên mẫu của hàm tạo của nó]
- Sau đó gọi Xe [] trong ngữ cảnh của đối tượng mới, có nghĩa là khi bắt gặp từ khóa “this” trong hàm tạo [xe []], nó đề cập đến đối tượng mới được tạo trong bước đầu tiên
- Khi điều này kết thúc, đối tượng mới được tạo sẽ được trả về car1 và car2 [trong ví dụ trên]
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à.
jav
let 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
jav
let 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
jav
let 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ụ.
jav
let 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.