JavaScript: The First Parts Part Three Douglas Crockford Yahoo! Inc.
Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }
-
Upload
steve-hernandez -
Category
Documents
-
view
229 -
download
4
Transcript of Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }
![Page 1: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/1.jpg)
Closure
Douglas Crockford
![Page 2: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/2.jpg)
Block Scope
{ let a; { let b; … a … … b … } … a …}
![Page 3: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/3.jpg)
Function Scope
function green() { let a; function yellow() { let b; … a … … b … } … a …}
![Page 4: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/4.jpg)
Function Scope
function green() { let a; function yellow() { let b; … a … … b … } … a …}
a
![Page 5: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/5.jpg)
Function Scope
function green() { let a; function yellow() { let b; … a … … b … } … a …}
a b
![Page 6: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/6.jpg)
• Lisp [1958] dynamic scope
nested functions
function values
• Algol 60 [1960] lexical scope
nested functions
functions are not values
• C [1972] lexical scope
functions cannot nest
functions are values
![Page 7: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/7.jpg)
Inner survives the outer
function green() { let a; return function yellow() { let b; … a … … b … }; … a …}
![Page 8: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/8.jpg)
Global
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
var digit_name = function (n) {
return names[n];
};
alert(digit_name(3)); // 'three'
![Page 9: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/9.jpg)
Slowvar digit_name = function (n) {
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
return names[n];
};
alert(digit_name(3)); // 'three'
![Page 10: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/10.jpg)
Closurevar digit_name = (function () {
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
return function (n) {
return names[n];
};
}());
alert(digit_name(3)); // 'three'
![Page 11: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/11.jpg)
Start Over
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
var digit_name = function (n) {
return names[n];
};
alert(digit_name(3)); // 'three'
![Page 12: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/12.jpg)
Immediate function returns a function
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
var digit_name = (function () {
return function (n) {
return names[n];
};
}());
alert(digit_name(3)); // 'three'
![Page 13: Closure Douglas Crockford. Block Scope { let a; { let b; … a … … b … } … a … }](https://reader030.fdocuments.in/reader030/viewer/2022013115/56649c735503460f94926492/html5/thumbnails/13.jpg)
Closurevar digit_name = (function () {
var names = ['zero', 'one', 'two',
'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
return function (n) {
return names[n];
};
}());
alert(digit_name(3)); // 'three'