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版

第10章 匿名関数と高階関数:解放された関数

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

Image

関数を知り、そしてロックせよ。どんな芸術、技術、分野にも、中級者とロックスターの名人芸を分ける重要な原則がある。関数はJavaScriptの基本であり、コードの設計や組織化に使うテクニックの多くは、関数の高度な知識と使い方に依存している。次の2章は、ウィリー・ウォンカのチョコレート工場ツアーに参加するようなもので、JavaScriptの関数についてより深く学ぶにつれて、ワイルドで奇抜で素晴らしいものに遭遇することになる。

ウンパ・ルンパの歌は割愛する。

forキーワードの不思議な二重生活

これまでは、このように関数を宣言してきた:

Image

形式的には、上の最初の文は関数宣言で、関数を作成し、その関数を参照したり呼び出したりするのに使える名前(この場合はquack)を付ける。

ここまではいいのだが、前章の最後で見たように、functionキーワードにはもうひとつの使い方があるからだ:

Image

さて、このようにfunctionキーワードを使うとき、つまり代入文のように文の中で使うとき、これを関数式と呼び出す。関数宣言とは異なり、この関数には名前がない。また、式の結果、値が変数fly に代入される。その値とは何か?変数fly に代入し、それを後で呼び出すのだから、関数への参照に違いない。

関数もまた価値である

しかし、関数を値として考えることもできる。この値は実際には関数への参照であり、これまで見てきたように、関数宣言で関数を作成しても、関数式で関数を作成しても、その関数への参照を得ることになる:

関数を、数値、文字列、ブール値、オブジェクトと同じように、値として考え始めよう。関数の値が他の値と決定的に違うのは、関数を呼び出すことができるということだ

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