SlideShare a Scribd company logo
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

C++ programs
C++ programsC++ programs
C++ programs
Mukund Gandrakota
 
week-4x
week-4xweek-4x
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
HarithaRanasinghe
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
cexpertise
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
Koshy Geoji
 
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
Dr. Loganathan R
 
week-8x
week-8xweek-8x
2 d rotation
2 d rotation2 d rotation
2 d rotation
Chandu Kumare
 
C program to add two numbers
C program to add two numbers C program to add two numbers
C program to add two numbers
mohdshanu
 
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
Dr. Loganathan R
 
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
Dr. Loganathan R
 
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
Dr. Loganathan R
 
C workshop day 6
C workshop day 6C workshop day 6
C workshop day 6
Mayank Agrawal
 
Basic program in java
Basic program in java Basic program in java
Basic program in java
Sudeep Singh
 
Project filter matlab
Project  filter matlabProject  filter matlab
Project filter matlab
girma disasa
 
Ffffffffffff
FfffffffffffFfffffffffff
Ffffffffffff
mohdshanu
 
MFC Polygon
MFC PolygonMFC Polygon
Metnum
MetnumMetnum
Metnum
ratnaaning
 

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

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

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 programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
Rupendra Choudhary
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
Saket 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/c2go
Moriyoshi Koizumi
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket 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.pptx
ssuser3cbb4c
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
EasyStudy3
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# Developers
Rick Beerendonk
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
happycocoman
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
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
aptcomputerzone
 
pointers 1
pointers 1pointers 1
pointers 1
gaurav koriya
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
Export Promotion Bureau
 
Day 1
Day 1Day 1
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
Farhan Ab Rahman
 
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
Ismar Silveira
 
Function recap
Function recapFunction recap
Function recap
alish sha
 
Function recap
Function recapFunction recap
Function recap
alish 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

Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
Data Hops
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
Edge AI and Vision Alliance
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 

Recently uploaded (20)

Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 

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