转载地址:http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/

Introduction

In this article we will talk about one of the most discussed topics related with JavaScript — aboutclosures. The topic, as a matter of fact, is not new and was discussed many times. However we will try to discuss and understand it more from theoretical point of view, and also will look at how closures are made in ECMAScript from within.

Two previous chapters devoted to scope chain and variable object can be good to consider first, since in this chapter we will use material discussed earlier.

General theory

Before the discussion of closures directly in ECMAScript, it is necessary to specify a number of definitions from the general theory of functional programming.

As is known, in functional languages (and ECMAScript supports this paradigm and stylistics), functions are data, i.e. they can be assigned to variables, passed as arguments to other functions,returned from functions etc. Such functions have special names and structure.

Definitions

functional argument (“Funarg”) — is an argument which value is a function.

Example:

function exampleFunc(funArg) {
  funArg();
}
 
exampleFunc(function () {
  alert('funArg');
});

相关文章: