0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【JavaScript】コールバック関数の型【メモ】

Last updated at Posted at 2020-11-25

第一引数と第二引数の値を処理する関数

function doSomething(a, b, callback) {
  const result = callback(a, b)
  console.log(result)
}
// 掛け算をする関数
function multiply(a, b) {
  return a * b
}
// 足し算をする関数
function plus(a, b) {
  return a + b
}
// ...

// doSomething関数を実行すると、第一引数の2と第二引数の2を掛け算し、4が出力される
doSomething(2, 2, multiply)

// 第3引数にplusを入れた場合は、2+3が実行され、5が出力される
doSomething(2, 3, plus)

思ったこと

最初に、今回でいう「doSomething()」のような関数を用意しておくことで、後々に別の機能(例えば引き算させる関数「minus」など)を追加する際に、記述量が少なく済み、可読性も上がると感じました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?