الكائنات
تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
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.
![const pet = { name: "Rex", age: 3 }; pet.name is "Rex", pet["age"] is 3, pet.age = 4 changes the age](/static/code/javascript/object.png)
English
let person = { name: "Ada", age: 36 };
console.log(person.name); // → Ada
console.log(person.age); // → 36
استكشف
A key looks up a value
An object maps each بحث مفتاح القاموس إلى القيمة؛ التفاصيل في البطاقات أدناه. to a القيمة. obj.key looks one up instantly.
English
Change or add a property
- Assign to
obj.keyto change a value — or to add a brand-new one.
English
let car = { speed: 0 };
car.speed = 60; // change an existing key
car.colour = "red"; // add a new key
console.log(car.speed); // → 60
English
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.
English
Common mistakes
- Read a value with
obj.keyorobj["key"]. - A missing key gives
undefined, not an error.
English
Now you try
- Read and change object properties.
- Press Check answer to test it.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
let book = { title: "CS", pages: 200 }. Log the book's title with book.title (it is CS).
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
let car = { speed: 0 }. Change car.speed إلى 80, then log it.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Create an object p with a property x set to 3 and a property y set to 4.
اضغط تشغيل لرؤية المخرجات هنا.