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 1 year has passed since last update.

typescriptのcallback関数がぱっと見が謎だった

Posted at

udemyの講座を見ていてぱっと見謎だった。
console.logで42が表示されるソースが以下。

index.ts
function doubleAndHandle(num: number, cb: (num: number) => number) : void {
  const doubleNum = cb(num * 2);
  console.log(doubleNum);
}
doubleAndHandle(21, doubleNum => {
  return doubleNum;
});

googleでアロー関数を調べて理解した。要は無名関数の以下と同義。

index.ts
doubleAndHandle(21, function (doubleNum) {
  return doubleNum;
});

()を付けたほうがわかりやすいと思った。

index.ts
doubleAndHandle(21, (doubleNum) => {
  return doubleNum;
});
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?