SlideShare a Scribd company logo
1 of 18
JavaScript Training
Goal Trainers Format
• Lecture
• Exercises
• Ask Questions!
• bitovi/js-training
Functions & Closures
var sum = function( x , y ){
return (++x) +
(++y.val)
},
a = 1,
b = {val : 2},
c = sum( a, b);
Types as Arguments
WINDOW
sum fn
a
b
c 5
1
2
sum(a,b
)
x
y
1
PROTOTYPE
val
2
3
Summary
Primitives: passed by value, get a copy of
data
Objects: passed by reference
var a = {};
for(var i=0; i<3; i++) {
a[i] = function() { alert(i) };
}
a[0]();
a[1]();
a[2]();
// 3
// 3
// 3
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count 0
fnc1
c1()
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count 0
fnc1
c1()
1
// 1
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count
fnc1
1
// 1
c1()
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count
fnc1
c1()
1
// 1
2
// 2
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count
fnc1
c1()
// 1
2
// 2
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count
fnc1
2
c2 fn
counter()
c2()
0count
// 1
// 2
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count
fnc1
2
c2 fn
counter()
c2()
0count 1
// 1
// 2
var counter = function(){
var count = 0;
return function(){
return ++count;
};
};
var c1 = counter();
c1();
c1();
var c2 = counter();
c2();
WINDOW
counter fn
counter()
count
fnc1
2
c2 fn
counter()
c2()
count 1
// 1
// 1
// 2
1
var a = {};
for(vari=0;i<3;i++){
a[i] = function(){
alert(i)
};
}
a[0]();
a[1]();
a[2]();
Closure Gotchas
WINDOW
0
2
i
0
fn
a
fn
fn
123
a[0]()
1
var a = {};
for(vari=0;i<3;i++){
a[i] = function(){
alert(i)
};
}
a[0]();
a[1]();
a[2]();
Closure Gotchas
WINDOW
0
2
i
0
fn
a
fn
fn
123
a[0]()a[0]()
WINDOW
i
3
2
fn 2…
…1 fn 1
Closure Gotchas
WINDOW
0
a
fn()
j
i
0
0
var a = {};
for(var i=0; i<3; i++){
(function(j){
a[j] = function(){
alert(j)
};
})(i);
}
a[0]();
a[1]();
a[2]();
fn
fn
123
a[0]()
fn()
j
Exercise
Build a tag library that creates elements of the following types in the
least LOC: a, div, span, form, h1, h2, h3, h4.
Example usage:
var h1 = make.h1();
h1.innerHTML = 'Hello World';
document.body.appendChild(h1);
var a = make.a();
a.href= 'http://canjs.com';
a.innerHTML = 'CanJS';
document.body.appendChild(a);
HINTS:
• Code is ‘built’ one statement at a time.
• document.createElement('h1') – creates and returns an 'h1' element.
fn(me,val)
this
arg1
arg2
Summary
Creating a Function
var fn=function(arg1,arg2){
var foo = "Bar";
}
var me = {name: "Justin"},
val= 5
fn(me, val)
Calling a Function
WINDOW
fn fn
PROTOTYPE
me "Justin"
name
val 5
5
"Bar"foo

More Related Content

What's hot

What's hot (19)

C++ programs
C++ programsC++ programs
C++ programs
 
week-4x
week-4xweek-4x
week-4x
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
week-8x
week-8xweek-8x
week-8x
 
2 d rotation
2 d rotation2 d rotation
2 d rotation
 
C program to add two numbers
C program to add two numbers C program to add two numbers
C program to add two numbers
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointers
 
Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1
 
Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1
 
C workshop day 6
C workshop day 6C workshop day 6
C workshop day 6
 
Basic program in java
Basic program in java Basic program in java
Basic program in java
 
Project filter matlab
Project  filter matlabProject  filter matlab
Project filter matlab
 
Ffffffffffff
FfffffffffffFfffffffffff
Ffffffffffff
 
MFC Polygon
MFC PolygonMFC Polygon
MFC Polygon
 
Metnum
MetnumMetnum
Metnum
 

Viewers also liked

Types, Operators and Primitives
Types, Operators and PrimitivesTypes, Operators and Primitives
Types, Operators and Primitivesalexisabril
 
Element Styles and Positioning
Element Styles and PositioningElement Styles and Positioning
Element Styles and Positioningalexisabril
 

Viewers also liked (7)

Prototypes
PrototypesPrototypes
Prototypes
 
Types, Operators and Primitives
Types, Operators and PrimitivesTypes, Operators and Primitives
Types, Operators and Primitives
 
Comparisons
ComparisonsComparisons
Comparisons
 
Context
ContextContext
Context
 
Element Styles and Positioning
Element Styles and PositioningElement Styles and Positioning
Element Styles and Positioning
 
Events - Part 2
Events - Part 2Events - Part 2
Events - Part 2
 
Basic JS
Basic JSBasic JS
Basic JS
 

Similar to Closures

C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in CSaket Pathak
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersRick Beerendonk
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfaptcomputerzone
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Function recap
Function recapFunction recap
Function recapalish sha
 

Similar to Closures (20)

C lab programs
C lab programsC lab programs
C lab programs
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# Developers
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
pointers 1
pointers 1pointers 1
pointers 1
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
Day 1
Day 1Day 1
Day 1
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Closures

  • 1. JavaScript Training Goal Trainers Format • Lecture • Exercises • Ask Questions! • bitovi/js-training
  • 3. var sum = function( x , y ){ return (++x) + (++y.val) }, a = 1, b = {val : 2}, c = sum( a, b); Types as Arguments WINDOW sum fn a b c 5 1 2 sum(a,b ) x y 1 PROTOTYPE val 2 3
  • 4. Summary Primitives: passed by value, get a copy of data Objects: passed by reference
  • 5. var a = {}; for(var i=0; i<3; i++) { a[i] = function() { alert(i) }; } a[0](); a[1](); a[2](); // 3 // 3 // 3
  • 6. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count 0 fnc1 c1()
  • 7. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count 0 fnc1 c1() 1 // 1
  • 8. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count fnc1 1 // 1 c1()
  • 9. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count fnc1 c1() 1 // 1 2 // 2
  • 10. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count fnc1 c1() // 1 2 // 2
  • 11. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count fnc1 2 c2 fn counter() c2() 0count // 1 // 2
  • 12. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count fnc1 2 c2 fn counter() c2() 0count 1 // 1 // 2
  • 13. var counter = function(){ var count = 0; return function(){ return ++count; }; }; var c1 = counter(); c1(); c1(); var c2 = counter(); c2(); WINDOW counter fn counter() count fnc1 2 c2 fn counter() c2() count 1 // 1 // 1 // 2
  • 14. 1 var a = {}; for(vari=0;i<3;i++){ a[i] = function(){ alert(i) }; } a[0](); a[1](); a[2](); Closure Gotchas WINDOW 0 2 i 0 fn a fn fn 123 a[0]()
  • 15. 1 var a = {}; for(vari=0;i<3;i++){ a[i] = function(){ alert(i) }; } a[0](); a[1](); a[2](); Closure Gotchas WINDOW 0 2 i 0 fn a fn fn 123 a[0]()a[0]() WINDOW i 3
  • 16. 2 fn 2… …1 fn 1 Closure Gotchas WINDOW 0 a fn() j i 0 0 var a = {}; for(var i=0; i<3; i++){ (function(j){ a[j] = function(){ alert(j) }; })(i); } a[0](); a[1](); a[2](); fn fn 123 a[0]() fn() j
  • 17. Exercise Build a tag library that creates elements of the following types in the least LOC: a, div, span, form, h1, h2, h3, h4. Example usage: var h1 = make.h1(); h1.innerHTML = 'Hello World'; document.body.appendChild(h1); var a = make.a(); a.href= 'http://canjs.com'; a.innerHTML = 'CanJS'; document.body.appendChild(a); HINTS: • Code is ‘built’ one statement at a time. • document.createElement('h1') – creates and returns an 'h1' element.
  • 18. fn(me,val) this arg1 arg2 Summary Creating a Function var fn=function(arg1,arg2){ var foo = "Bar"; } var me = {name: "Justin"}, val= 5 fn(me, val) Calling a Function WINDOW fn fn PROTOTYPE me "Justin" name val 5 5 "Bar"foo

Editor's Notes

  1. I think the key to JavaScript is really understanding what’s going on in memory. And the key to understanding what’s going on in memory is understanding what JS’s basic data types look like in memory and how JS’s operators are used to manipulate those data structures.
  2. Every time you create a function, a new closure is created
  3. call object closure stack frame activation frame