I’m reading Douglas Crockford’s book “JavaScript: The Good Parts”. I can’t understand the sentence that “Functions in Simplified JavaScript are lambdas with lexical scoping.”. What does this sentence want to tell us? How do you understand this sentence?
Thank you very much!
Below is the contexts of this sentence in the book:
I wanted to include all of the code
for a parser in JavaScript that parses
JavaScript. But my chapter was just
one of 30 or 40, so I felt constrained
in the number of pages I could
consume. A further complication was
that most of my readers would have no
experience with JavaScript, so I also
would have to introduce the language
and its peculiarities.So, I decided to subset the language.
That way, I wouldn’t have to parse the
whole language, and I wouldn’t have to
describe the whole language. I called
the subset Simplified JavaScript.
Selecting the subset was easy: it
included just the features that I
needed to write a parser. This is how
I described it in Beautiful Code:Simplified JavaScript is just the good
stuff, including:Functions as firortranst class objects
Functions in Simplified JavaScript are lambdas with lexical scoping.
Dynamic objects with prototypal
inheritanceObjects are class-free. We can add a
new member to any object by ordinary
assignment. An object can inherit
members from another object.Object literals and array literals
This is a very convenient notation for
creating new objects and arrays.
JavaScript literals were the
inspiration for the JSON data
interchange format.
He’s attempting to explain the nature of function declarations in JavaScript. By describing them as “lambdas with lexical scoping” he’s eluding to the number of ways they can be used. You can, for example, declare them statically:
You can store them in a variable:
You can use them as object properties (methods):
Or you can declare them as you pass them somewhere (very common when working with jQuery):
Because of the flexibility in when / how they’re declared they fill the same role that lambdas do in many other languages (Python for example).