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版

第11章. 名前空間パターン

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

この章では、JavaScriptにおけるネームスペースのパターン を探る。名前空間は、一意な識別子の下にコード単位を論理的にグループ化したものと考えることができる。多くの名前空間で識別子を参照することができ、各識別子はネストされた(またはサブ)名前空間の階層を含むことができる。

アプリケーション開発では、多くの重要な理由から名前空間を採用する。JavaScriptの名前空間は、グローバル名前空間にある他のオブジェクトや変数との衝突を避けるのに役立つ。また、コードベース内の機能のブロックを整理して、より簡単に参照・使用できるようにするのにも便利だ。

本格的なスクリプトやアプリケーションの名前付けは非常に重要である。なぜなら、ページ上の他のスクリプトが私たちと同じ変数名やメソッド名を使用した場合に、私たちのコードが壊れないように保護することが重要だからだ。サードパーティのタグが定期的にページに注入されるため、これは私たちのキャリアのある時点で誰もが取り組む必要のある一般的な問題になり得る。グローバル・ネームスペースのお行儀の良い「市民」として、同じ問題によって他の開発者のスクリプトの実行を妨げないように最善を尽くすことも必須だ。

JavaScriptには、他の言語のような名前空間のビルトイン・サポート()はないが、同様の効果を得るために使えるオブジェクトやクロージャがある。

ネームスペースの基礎

JavaScriptアプリケーションのほとんどで、名前空間を見つけることができる。単純なコード・スニペットで作業しているのでない限り、名前空間が正しく実装されていることを確認するために最善を尽くさなければならない。このセクションで検討するパターンは以下の通りだ:

  • 単一のグローバル変数

  • 接頭辞の名前空間

  • オブジェクト・リテラル表記法

  • 入れ子の名前空間

  • 即座に呼び出される関数

  • 名前空間インジェクション

単一のグローバル変数

JavaScriptで 、名前空間を指定するためのよく使われるパターンのひとつは、参照するオブジェクトを単一のグローバル変数にすることだ。以下は、関数とプロパティを持つオブジェクトを返す、この実装のスケルトンである:

const myUniqueApplication = (() => {
  function myMethod() {
    // code
    return;
  }

  return {
    myMethod,
  };
})();

// Usage
myUniqueApplication.myMethod();

// In this updated example, we use an immediately invoked function expression
// (IIFE) to create a unique namespace for our application, which is stored in
// the myUniqueApplication variable. The IIFE returns an object with functions
// and properties, and we can access these using dot notation
// (e.g., myUniqueApplication.myMethod()). ...
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