LoginSignup
0
0

More than 3 years have passed since last update.

ローランドの名言をコールバック関数を使って出力してみる

Last updated at Posted at 2020-07-08

Javascriptにおいて
関数を変数として扱う場合も多いらしいので、
いろんな方法でローランドの発言を出力してみる。

通常

console.log('俺か、俺以外か');

関数

function roland(){
  console.log('俺か,俺以外か');
}

roland();

コールバック関数(変数代入)

function roland(callback) {
  console.log(callback());
}

const remark = function() {
  return '俺か俺以外か';
}

roland(remark);

console.log(callback());
は関数の実行結果が返ってくる

console.log(callback);
は関数自体が返ってくる

コールバック関数(無名関数)

function roland(callback) {
  console.log(callback());
}

roland( function() {
  return '俺か、俺以外か';
})

コールバック関数(文字列結合)

function roland(me, otherThanThat) {
  console.log(me(otherThanThat))
}

roland( function(otherThanThat) {
  return '俺か' + otherThanThat;
}, '俺以外か');

ここ一番ややこしい。。

コールバック関数(演算)

function roland(a, b, callback) {
  const result = callback(a, b);
  console.log(result);
}

function remark(a, b) {
  return a + b;
}

roland('俺か', '俺以外か', remark);
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