Understanding closure in Javascript

后端 未结 8 713
不思量自难忘°
不思量自难忘° 2020-12-28 23:19

I\'m trying to wrap my head around closures in Javascript.

Here is an example from a tutorial:

function greeter(name, age) {
  var message = name + \         


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 23:39

    Once you "get it" you will wonder why it took you so long to understand it. That's the way way I felt anyways.

    I think function scope in Javascript can be expressed fairly concisely.

    The function body will have access to any variables that were visible in the lexical environment of the function declaration, and also any variables created via the function's invocation -- that is, any variables declared locally, passed through as arguments or otherwise provided by the language (such as this or arguments).

提交回复
热议问题