Objects
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(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.
日本語
オブジェクトは関連するデータをグループ化する
- オブジェクトは名前付きの値を一緒に格納します:
{ key: value, ... }。 - ドットを使ってキーで値にアクセスします:
person.name。 - オブジェクトは、詳細が多数ある1つの対象に最適です。
let person = { name: "Ada", age: 36 };
console.log(person.name); // → Ada
console.log(person.age); // → 36
Explore · 探索
A key looks up a value
An object maps each key to a value. 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.
日本語
プロパティの変更または追加
obj.keyに代入すると値を変更するか、全く新しいものを追加できます。
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.
日本語
注意
- 存在しないキーはエラーではなく
undefinedを返します:person.height→undefined。 person["name"]はperson.nameと同じ結果になります。キーが変数にある場合に便利です。
English
Common mistakes
- Read a value with
obj.keyorobj["key"]. - A missing key gives
undefined, not an error.
日本語
よくあるミス
obj.keyまたはobj["key"]で値を読み取ります。- 存在しないキーはエラーではなく
undefinedを返します。
English
Now you try
- Read and change object properties.
- Press Check answer to test it.
日本語
あなたも試してみよう
- オブジェクトのプロパティを読み取り、変更してください。
- Check answer を押して確認する。
let book = { title: "CS", pages: 200 }. Log the book's title with book.title (it is CS).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
let car = { speed: 0 }. Change car.speed to 80, then log it.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Create an object p with a property x set to 3 and a property y set to 4.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。