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版

第11章. 現代文法、レキシカル・スコープ、クロージャ:深刻な関数

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

Image

この章では、さらに本格的な関数を紹介する。引数やパラメータ、代入を扱う高度なテクニックを使って、構文のレベルを上げる方法を紹介する。さらに、JavaScriptがどのようにスコープを管理するのか、その細かい点についても見ていく。このスコープの微妙な違いを探る旅は、しばしば謎に包まれがちだが、JavaScript を使いこなす上で極めて重要な概念であるクロージャの核心へと私たちを導いてくれる。最終的には、あなたが想像していた以上にJavaScriptの式が表現できるようになるだろう。

そして、もしあなたが閉鎖という言葉を聞いたことがあっても、それが何なのかよく知らないのなら、あなたは正しい場所にいる!

関数構文に本腰を入れる

この時点で、関数の基本的な構文について知っておくことはもう何もないと思うかもしれないが、実はJavaScriptには、関数の使い方をよりわかりやすく、表現力豊かにするために追加された構文の拡張がいくつもある。スコープとクロージャの話に入る前に、関数を呼び出したり、関数から値を受け取ったりするときにできることをすべて確認しておこう。

Image

デフォルト・パラメータでコードを合理化する

メッセージと受信者を受け取る関数greeting があるとしよう:

function greeting(message, recipient) {
   console.log(message, recipient);
}

greeting 、こう呼びかけよう:

Image

こんな風に引数を外してしまったら、何が得られると思う?

Image

JavaScriptはデフォルトのパラメータ値を提供することで、これに対処する方法を提供している。呼び出し元が引数を渡さなかった場合、undefined の代わりにそのデフォルト値が使われる。 このように動作する:

Image

試してみよう:

どのパラメータにもデフォルト値を指定することができる。ここでは、greeting の両パラメータにデフォルト値を指定する:

function greeting(message="Howdy", ...
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