Objects · Objek
Objects group related data
- An object stores named values together:
{ key: value, ... }. - Reach a value by its key with a dot:
person.name. - An object is perfect for one thing that has several details.
Objek mengelompokkan data terkait
- Objek menyimpan nilai bernama bersama-sama:
{ key: value, ... }. - Akses nilai berdasarkan kuncinya dengan titik:
person.name. - Objek sangat cocok untuk satu benda yang memiliki beberapa detail.
let person = { name: "Ada", age: 36 };
console.log(person.name); // → Ada
console.log(person.age); // → 36
A key looks up a value · Kunci melihat nilai
An object maps each key to a value. obj.key looks one up instantly. · Sebuah objek memetakan setiap kunci ke nilai. obj.key melihatnya seketika.
Change or add a property
- Assign to
obj.keyto change a value — or to add a brand-new one.
Mengubah atau menambah properti
- Berikan nilai ke
obj.keyuntuk mengubah nilai — atau menambah yang benar-benar baru.
let car = { speed: 0 };
car.speed = 60; // change an existing key
car.colour = "red"; // add a new key
console.log(car.speed); // → 60
Watch out
- A missing key gives
undefined, not an error:person.height→undefined. person["name"]does the same asperson.name— handy when the key sits in a variable.
Hati-hati
- Kunci yang hilang menghasilkan
undefined, bukan kesalahan:person.height→undefined. person["name"]melakukan hal yang sama denganperson.name— berguna ketika kunci berada dalam variabel.
Common mistakes
- Read a value with
obj.keyorobj["key"]. - A missing key gives
undefined, not an error.
Kesalahan umum
- Baca nilai dengan
obj.keyatauobj["key"]. - Kunci yang hilang menghasilkan
undefined, bukan kesalahan.
Now you try
- Read and change object properties.
- Press Check answer to test it.
Sekarang Anda coba
- Baca dan ubah properti objek.
- Tekan Cek jawaban untuk mengujinya.
let book = { title: "CS", pages: 200 }. Log the book's title with book.title (it is CS). · let book = { title: "CS", pages: 200 }. Log properti title dari buku dengan book.title (nilainya adalah CS).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
let car = { speed: 0 }. Change car.speed to 80, then log it. · let car = { speed: 0 }. Ubah car.speed menjadi 80, lalu log.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Create an object p with a property x set to 3 and a property y set to 4. · Buat objek p dengan properti x diatur ke 3 dan properti y diatur ke 4.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.