Skip to Content
Head First JavaScriptプログラミング、第2版
book

Head First JavaScriptプログラミング、第2版

by Eric Freeman, Elisabeth Robson
March 2025
Intermediate to advanced
662 pages
13h 28m
Japanese
O'Reilly Media, Inc.
Content preview from Head First JavaScriptプログラミング、第2版

第12章. 高度なオブジェクト構築:オブジェクトの作成

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

Image

これまでは、オブジェクトを手作業で作ってきた。各オブジェクトについて、オブジェクト・リテラルを使って一つひとつのプロパティを指定してきた。小規模ならそれでもいいが、本格的なコードにはもっといいものが必要だ。そこでクラスの登場だ。クラスを使えば、もっと簡単にオブジェクトを作成することができ、同じ設計図に従ったオブジェクトを作成することができる。クラスを使えば、オブジェクト・コードをより簡潔に書くことができ、たくさんのオブジェクトを作成するときのエラーも少なくなる。では、始めよう。

オブジェクト・リテラルを使ってオブジェクトを作成する

本書ではこれまで、オブジェクトの作成にオブジェクト・リテラルを使ってきた。オブジェクト・リテラルを使えば、オブジェクトを文字通り書き出すことで作成できる。そう、文字通りだ:

let taxi = { 
    make: "Webville Motors",
    model: "Taxi",
    year: 1955,
    color: "yellow",
    passengers: 4,
    convertible: false,
    mileage: 281341, 
    started: false,

    start() { this.started = true;},
    stop() { this.started = false;},
    drive() {
         // drive code here
    }
};
Image

オブジェクト・リテラルを使えば、コード内のどこにでもオブジェクトを作成することができる。しかし、たくさんのオブジェクトを作成する必要がある場合、例えばタクシーの車両を作成する場合、100種類ものオブジェクト・リテラルを入力する必要はないだろう。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技術リーダーシップのための14のヒント

技術リーダーシップのための14のヒント

島田 浩二
カオスエンジニアリング ―回復力のあるシステムの実践

カオスエンジニアリング ―回復力のあるシステムの実践

Casey Rosenthal, Nora Jones, 堀 明子, 松浦 隼人
Tidy First? ―個人で実践する経験主義的ソフトウェア設計

Tidy First? ―個人で実践する経験主義的ソフトウェア設計

Kent Beck, 吉羽 龍太郎, 永瀬 美穂, 細澤 あゆみ

Publisher Resources

ISBN: 9798341633186