Menu
Coddy logo textTech

オブジェクトのクイック復習

CoddyのJavaScriptジャーニー「オブジェクト指向プログラミング」セクションの一部 — レッスン 1/56。

データオブジェクトからスマートオブジェクトへ

セクション2「ロジックとフロー」では、オブジェクトをデータコンテナとして使用する方法を学びました。ここからは、これらの静的なデータオブジェクトを、アクションを実行し、自身の状態を管理できる動的でインテリジェントなオブジェクトへと変換していきます。

セクション2で既に学んだこと:
オブジェクトは、関連するデータをまとめて保存できるキーと値のペアの集合です。何かに関する情報を保持するコンテナのようなものだと考えてください。

// シンプルなデータコンテナとしてのオブジェクト
const player = {
   name: "Alex",
   health: 100,
   level: 2,
   inventory: ["sword", "potion"]
};

// データへのアクセス
console.log(player.name);          // "Alex" 
console.log(player.health);        // 100 
console.log(player.inventory[0]);  // "sword" - 配列の要素にアクセス

// データの更新
player.health = 85;
challenge icon

チャレンジ

player の level をコンソールに出力してください。

チートシート

オブジェクトは、関連するデータをキーと値のペアとして保存でき、ドット記法を使用してアクセスできます。

const player = {
   name: "Alex",
   health: 100,
   level: 2,
   inventory: ["sword", "potion"]
};

// オブジェクトのプロパティへのアクセス
console.log(player.name);          // "Alex" 
console.log(player.health);        // 100 
console.log(player.inventory[0]);  // "sword"

// オブジェクトのプロパティの更新
player.health = 85;

自分で試してみよう

const player = {
   name: "Alex",
   health: 100,
   level: 2,
   inventory: ["sword", "potion"]
};

// TODO: プレイヤーのレベルをコンソールに出力する
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

オブジェクト指向プログラミングのすべてのレッスン