LoginSignup
12
13

More than 5 years have passed since last update.

CoffeeScriptでクロージャ関数をつくる

Posted at

クロージャとは、「静的な変数を持つ関数」
その関数を繰り返し呼んだとしても、
ある変数の値をリセットせずに保持し続ける。

closure = ->

  x = 0 //これが静的変数となる。

  //関数で関数を返す。
  //この関数に処理をかく。
  return childFanc = ->
    x++
    alert x
closureTest = ->
  //クロージャ関数の生成
  clsr = closure()

  clsr() # 1
  clsr() # 2
  clsr() # 3
  clsr() # 4
  clsr() # 5
12
13
1

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
12
13