Skip to Content
JavaScriptデザインパターンを学ぶ 第2版
book

JavaScriptデザインパターンを学ぶ 第2版

by Addy Osmani
March 2025
Intermediate to advanced
298 pages
4h 18m
Japanese
O'Reilly Media, Inc.
Content preview from JavaScriptデザインパターンを学ぶ 第2版

第7章. JavaScriptデザインパターン

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

前の章では、3つの異なるカテゴリーのデザインパターンの例を示した。これらのデザインパターンの中には、ウェブ開発の文脈に関連するもの、あるいは必要とされるものもある。JavaScriptに適用すると役立つ、時代を超越したパターンもいくつか確認した。この章では、さまざまな古典的でモダンなデザインパターンのJavaScript実装を探る。すべてのセクションは、3つのカテゴリー(創造的、構造的、振る舞い)のうちの1つに特化している。まず、創造的パターンから始めよう

創造的パターン

作成パターンは、オブジェクトを作成するメカニズムを提供する。以下のパターンを取り上げる:

コンストラクタ・パターン

コンストラクタとは、新しく作成されたオブジェクトのメモリが割り当てられた後、初期化するために使用される特殊メソッドである のことである。ES2015+で、コンストラクタでクラスを作成する構文がJavaScriptに導入された。これにより、デフォルトのコンストラクタを使用して、クラスのインスタンスとしてオブジェクトを作成できるようになった。

JavaScriptでは、ほとんどすべてがオブジェクトであり、クラスはJavaScriptの継承に対するプロトタイプ的アプローチのための構文上の糖分である。古典的なJavaScriptでは、オブジェクトのコンストラクタに興味を持つことがほとんどだった。図7-1はそのパターンを示している。

オブジェクトのコンストラクタは、 、特定の型のオブジェクトを作成するために使用される。オブジェクトを使用するための準備をしたり、オブジェクトが最初に作成されたときにメンバープロパティとメソッドの値を設定するための引数を受け取ったりする。

ljd2 0701
図7-1. コンストラクタのパターン

オブジェクトの作成

JavaScriptで新しいオブジェクトを作成する一般的な方法は、以下の3つである:

// Each of the following options ...
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

アルゴリズムクイックリファレンス 第2版

アルゴリズムクイックリファレンス 第2版

George T. Heineman, Gary Pollice, Stanley Selkow, 黒川 利明, 黒川 洋
Node.jsデザインパターン 第2版

Node.jsデザインパターン 第2版

Mario Casciaro, Luciano Mammino, 武舎 広幸, 阿部 和也

Publisher Resources

ISBN: 9798341625952