Friday, February 26, 2010

Vinyl Stained By Rubber

Closures in JavaScript

After I have examples of closures (function accounts) in Scala and JavaFX had imagined, let's look the same example in JavaScript (ECMAScript ) to:
 / / closures.js 

zaehlerMitStartwert function (start value) {
return function () {return
starting value + +
}}


next1 = zaehlerMitStartwert (10)
next2 = zaehlerMitStartwert (20)

document.writeln (next1 ( ) document.writeln) / / 10
(next2 document.writeln ()) / / 20
(next2 document.writeln ()) / / 21
(next1 ()) / / 11

Much more can be found to really to say except that it
  1. really is that simple (which can also lead to problems )
  2. since the first versions of JavaScript (and thus For over 10 years) is available and
  3. unlike JavaFX Script is dynamically typed, and Scala, also no additional local variable is needed because of the transfer parameters already starting value is a modifiable variable. Both are evaluated in terms of stable, maintainable software is not necessarily positive.
Many modern JavaScript libraries make heavy use of such a function literals and closures, such as jQuery .

0 comments:

Post a Comment