/
Closure Closure

Closure - PowerPoint Presentation

tatiana-dople
tatiana-dople . @tatiana-dople
Follow
404 views
Uploaded On 2015-11-08

Closure - PPT Presentation

Douglas Crockford Block Scope let a let b a b a Function Scope function green let a ID: 187384

var function names digit function var digit names return scope alert functions green yellow closure values lexical nested

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Closure" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

ClosureSlide2

Block Scope

{

let a;

{

let b;

a

… b …

}

… a …

}Slide3

Function Scope

function

green

() {

let a;

function

yellow

() {

let b;

a

… b …

}

… a …

}Slide4

Function Scope

function

green

() {

let a;

function

yellow() { let b; … a … … b … } … a …}

aSlide5

Function Scope

function

green

() {

let a;

function

yellow() { let b; … a … … b … } … a …}

a

bSlide6

Lisp [1958]

dynamic scope

nested functions

function values

Algol

60 [1960]

lexical scope

nested functionsfunctions are not valuesC [1972]lexical scopefunctions cannot nestfunctions are valuesSlide7

Inner survives the outer

function

green

() {

let a;

return

function

yellow() { let b; … a … … b … }; … a …

}Slide8

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"Slide9

Slow

var

digit_name

=

function (n) {

var names = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ]; return names[n];};alert(digit_name(3)); // "three"Slide10

Closure

var

digit_name

= (

function (n) {

var names = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ]; return function (n) { return names[n]; };}());alert(digit_name

(3)); // "three"Slide11

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"Slide12

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"Slide13

Closure

var

digit_name

= (

function (n) {

var names = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ]; return function (n) { return names[n]; };}());alert(digit_name

(3)); // "three"