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.

クロージャーについて

Last updated at Posted at 2021-12-30

はじめに

クロージャーの概念や実装をまとめます。

定義

クロージャー:外的スコープの変数を関数が参照を保持する仕組みのこと

具体例

main.js
const incrementCountFactory = () => {
  let count = 0;

  const incrementCount = () => {
    count++;
    console.log(`${count}回目です`)
  }

  return incrementCount;
}

const incrementCount = incrementCountFactory();
incrementCount(); // 1回目です
incrementCount(); // 2回目です
incrementCount(); // 3回目です

incrementCountFactory:countという変数とincrementCount関数を定義。戻り値はincrementCount関数を返す
incrementCount:外的スコープの変数countを使用してcountに1を追加する関数

おわりに

メモリやガベージコレクションのことも追記していきたい。

参考

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?