SlideShare a Scribd company logo
The global object,
execution contexts &
closures
in
JavaScript
© Hans de Rooij
hdr.is-a-geek.com
Introduction
● Even after studying JavaScript for quite some
time three subjects remained elusive;
– the global object
– execution contexts &
– closures
● Then I happened to stumble upon a great
article by Dmitry Soshnikov
– Read the full article (http://bit.ly/1jDSFBh)
Why these slides?
● Dmitri's article is great but, in my modest
opinion, can be challenging because of the
high information density
● Furthermore, although the article contains
illustrations, I wanted more visual aides to
help me understand the concepts at hand
● So this presentation is all about how I read
the article and the Visio diagrams I created
to help me understand the JavaScript global
object, execution contexts and closures
JavaScript global object
● Dimitri provides the following definition for the
JavaScript global object;
The global object is the object which is created
before entering any execution context; this object
exists in a single copy, its properties are
accessible from any place in the program and the
life cycle of the global object ends when the
program ends
JavaScript global object
● Global object build-ins;
– global properties like undefined, Infinity and NaN
– global functions like isNaN(), parseInt() and eval()
– constructor functions like Object(), Array(), ...
– global singleton objects like Math and JSON
● The properties of the global object are not
reserved words but deserve to be treated as
such
JavaScript global object
● To retrieve a reference to the global object it's
always possible to use;
var global = (function() {return this;})();
● In browsers the object reference window is
the global object's delegate
● The following is true in the global context;
– isNaN === global.isNaN; (only after above assignment)
– isNaN === window.isNaN; (when run in a browser)
– isNaN === this.isNaN; (more on this, pun intended, later)
JavaScript execution context
● Every bit of JavaScript code is evaluated in an
execution context (EC for short)
● When the JS engine begins executing code, it
first enters the global execution context
● An EC has a set of properties to facilitate the
processing of its associated code
JavaScript execution context
● Elements of a JavaScript program runtime
JavaScript execution context
● Program runtime in case a function f() is
declared besides variable a
● Please note that, in this example, function f()
is not invoked!
* For more on function
declaration click here
JavaScript execution context
● There are two main types of JavaScript code;
– global code
– function code
● There is only one global context and may be
many instances of function contexts
● Every function invocation creates a separate
function execution context
JavaScript EC stack
● JavaScript program runtime can be visualized
as an execution context stack
● The top of the stack is the active context
JavaScript EC stack
● Program runtime immediately before function
invocation
JavaScript EC stack
● Program runtime while processing the code of
function f
JavaScript scope chain
● A scope chain is a list of objects which is
searched for identifiers that appear in code
● A variable object is a container of data
associated with the execution context
● In the global context the variable object is the
global object
● An activation object is created every time a
function is invoked
● The activation object is then used as the
variable object of the function context
JavaScript scope chain
● Resolving variable b in function f
JavaScript scope chain
● Resolving variable a in function f
JavaScript closures
● Closures are functions that have access to
variables from another function's scope
● In JavaScript closures are most often created
using nested functions
● If a nested function (example ret_f) is returned
from another function (example times_x) and
references variables from the parent context
(example var x) then this function retains a
reference to the parent's scope as it was at
the moment of instantiation of the function
JavaScript closures
JavaScript closures
JavaScript closures
JavaScript closures
JavaScript closures
JavaScript closures
Addendum: this
● The value of this is a property of the
execution context
● The value of this is set when entering an
execution context and cannot be changed
● The value of this never participates in the
identifier resolution process
– i.e. a this reference in JS code is immediately
resolved as a property of the active execution
context without a scope chain lookup
Addendum: this
● In the global context this refers to the global
object
● In a function context;
– the this value may be different with every single
function call
– the this value of an EC depends on the form of
the call expression
– In strict mode functions invoked as functions
(rather than as methods) always have a this
value of undefined
Addendum: this
var global = (function() {return this;})();
var a = 3;
var obj_1 = {a: 4, f: func};
var obj_2 = {a: 5};
function sThis(tp) {
if(tp === global) {return "Global object";}
else if(tp === obj_1) {return "Object obj_1";}
else if(tp === obj_2) {return "Object obj_2";}
else {return "Hmmm ... ";}
}
function func() {
alert(this.a + ", " + sThis(this));
}
func(); //3, Global object
obj_1.f(); //4, Object obj_1
func.call(obj_2); //5, Object obj_2
Conclusion
● I highly recommend reading Dmitry Soshnikov
article on the global object, execution contexts,
closures and this
● This article helped me understand these
concepts and, as a result, improved my
JavaScript skills in general
● I hope this presentation and the included Visio
diagrams help you with your study of the above
mentioned topics
© Hans de Rooij hdr.is-a-geek.com

More Related Content

PDF
JavaScript Execution Context
ODP
JavaScript Object Oriented Programming Cheat Sheet
PDF
Introduction to Object Oriented Javascript
ODP
Introduction of Object Oriented JavaScript
PDF
JavaScript: Patterns, Part 3
PPTX
An introduction to Object Oriented JavaScript
PPTX
PDF
JavaScript: Patterns, Part 2
JavaScript Execution Context
JavaScript Object Oriented Programming Cheat Sheet
Introduction to Object Oriented Javascript
Introduction of Object Oriented JavaScript
JavaScript: Patterns, Part 3
An introduction to Object Oriented JavaScript
JavaScript: Patterns, Part 2

What's hot (20)

PPTX
Advanced Javascript
PPTX
Qtp training session IV
PPTX
JavaScript Beyond jQuery
PPTX
Declarative JavaScript concepts and implemetation
PDF
Advanced JavaScript - Internship Presentation - Week6
PDF
JavaScript operators
PPTX
Constructors and Destructors
ODP
Functional programming in Javascript
PDF
JavaScript for real men
PPTX
Functional programming with Ruby - can make you look smart
PDF
What’s new in Kotlin?
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
ODP
Functors, Applicatives and Monads In Scala
PDF
java script functions, classes
PDF
Callback Function
PPTX
Introduction to JavaScript Programming
PPTX
Functional programming and ruby in functional style
PDF
Introduction to RxJS
PPTX
Groovy / comparison with java
PDF
Constructors and Destructors
Advanced Javascript
Qtp training session IV
JavaScript Beyond jQuery
Declarative JavaScript concepts and implemetation
Advanced JavaScript - Internship Presentation - Week6
JavaScript operators
Constructors and Destructors
Functional programming in Javascript
JavaScript for real men
Functional programming with Ruby - can make you look smart
What’s new in Kotlin?
Asynchronous JavaScript Programming with Callbacks & Promises
Functors, Applicatives and Monads In Scala
java script functions, classes
Callback Function
Introduction to JavaScript Programming
Functional programming and ruby in functional style
Introduction to RxJS
Groovy / comparison with java
Constructors and Destructors
Ad

Similar to JavaScript global object, execution contexts & closures (20)

PDF
[2015/2016] JavaScript
PDF
JavaScript
PPT
Introduction to Javascript
KEY
Exciting JavaScript - Part I
PDF
(3) cpp procedural programming
PPTX
Object oriented java script
KEY
The JavaScript Programming Primer
PDF
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
PPT
PPTX
predefined and user defined functions
PPTX
Advanced JavaScript
PDF
Design patterns in javascript
PDF
Java Enterprise Edition
PPTX
Javascript internals
PPTX
Java For Automation
PPT
WebGL: GPU acceleration for the open web
PPTX
JS Essence
PPTX
Journey of a C# developer into Javascript
PPTX
Lecture 4- Javascript Function presentation
PDF
JavaScript For CSharp Developer
[2015/2016] JavaScript
JavaScript
Introduction to Javascript
Exciting JavaScript - Part I
(3) cpp procedural programming
Object oriented java script
The JavaScript Programming Primer
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
predefined and user defined functions
Advanced JavaScript
Design patterns in javascript
Java Enterprise Edition
Javascript internals
Java For Automation
WebGL: GPU acceleration for the open web
JS Essence
Journey of a C# developer into Javascript
Lecture 4- Javascript Function presentation
JavaScript For CSharp Developer
Ad

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Digital Strategies for Manufacturing Companies
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
assetexplorer- product-overview - presentation
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Introduction to Artificial Intelligence
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
How to Migrate SBCGlobal Email to Yahoo Easily
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
CHAPTER 2 - PM Management and IT Context
Digital Strategies for Manufacturing Companies
2025 Textile ERP Trends: SAP, Odoo & Oracle
assetexplorer- product-overview - presentation
Digital Systems & Binary Numbers (comprehensive )
Wondershare Filmora 15 Crack With Activation Key [2025
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction to Artificial Intelligence
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms II-SECS-1021-03
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
L1 - Introduction to python Backend.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
Upgrade and Innovation Strategies for SAP ERP Customers

JavaScript global object, execution contexts & closures

  • 1. The global object, execution contexts & closures in JavaScript © Hans de Rooij hdr.is-a-geek.com
  • 2. Introduction ● Even after studying JavaScript for quite some time three subjects remained elusive; – the global object – execution contexts & – closures ● Then I happened to stumble upon a great article by Dmitry Soshnikov – Read the full article (http://bit.ly/1jDSFBh)
  • 3. Why these slides? ● Dmitri's article is great but, in my modest opinion, can be challenging because of the high information density ● Furthermore, although the article contains illustrations, I wanted more visual aides to help me understand the concepts at hand ● So this presentation is all about how I read the article and the Visio diagrams I created to help me understand the JavaScript global object, execution contexts and closures
  • 4. JavaScript global object ● Dimitri provides the following definition for the JavaScript global object; The global object is the object which is created before entering any execution context; this object exists in a single copy, its properties are accessible from any place in the program and the life cycle of the global object ends when the program ends
  • 5. JavaScript global object ● Global object build-ins; – global properties like undefined, Infinity and NaN – global functions like isNaN(), parseInt() and eval() – constructor functions like Object(), Array(), ... – global singleton objects like Math and JSON ● The properties of the global object are not reserved words but deserve to be treated as such
  • 6. JavaScript global object ● To retrieve a reference to the global object it's always possible to use; var global = (function() {return this;})(); ● In browsers the object reference window is the global object's delegate ● The following is true in the global context; – isNaN === global.isNaN; (only after above assignment) – isNaN === window.isNaN; (when run in a browser) – isNaN === this.isNaN; (more on this, pun intended, later)
  • 7. JavaScript execution context ● Every bit of JavaScript code is evaluated in an execution context (EC for short) ● When the JS engine begins executing code, it first enters the global execution context ● An EC has a set of properties to facilitate the processing of its associated code
  • 8. JavaScript execution context ● Elements of a JavaScript program runtime
  • 9. JavaScript execution context ● Program runtime in case a function f() is declared besides variable a ● Please note that, in this example, function f() is not invoked! * For more on function declaration click here
  • 10. JavaScript execution context ● There are two main types of JavaScript code; – global code – function code ● There is only one global context and may be many instances of function contexts ● Every function invocation creates a separate function execution context
  • 11. JavaScript EC stack ● JavaScript program runtime can be visualized as an execution context stack ● The top of the stack is the active context
  • 12. JavaScript EC stack ● Program runtime immediately before function invocation
  • 13. JavaScript EC stack ● Program runtime while processing the code of function f
  • 14. JavaScript scope chain ● A scope chain is a list of objects which is searched for identifiers that appear in code ● A variable object is a container of data associated with the execution context ● In the global context the variable object is the global object ● An activation object is created every time a function is invoked ● The activation object is then used as the variable object of the function context
  • 15. JavaScript scope chain ● Resolving variable b in function f
  • 16. JavaScript scope chain ● Resolving variable a in function f
  • 17. JavaScript closures ● Closures are functions that have access to variables from another function's scope ● In JavaScript closures are most often created using nested functions ● If a nested function (example ret_f) is returned from another function (example times_x) and references variables from the parent context (example var x) then this function retains a reference to the parent's scope as it was at the moment of instantiation of the function
  • 24. Addendum: this ● The value of this is a property of the execution context ● The value of this is set when entering an execution context and cannot be changed ● The value of this never participates in the identifier resolution process – i.e. a this reference in JS code is immediately resolved as a property of the active execution context without a scope chain lookup
  • 25. Addendum: this ● In the global context this refers to the global object ● In a function context; – the this value may be different with every single function call – the this value of an EC depends on the form of the call expression – In strict mode functions invoked as functions (rather than as methods) always have a this value of undefined
  • 26. Addendum: this var global = (function() {return this;})(); var a = 3; var obj_1 = {a: 4, f: func}; var obj_2 = {a: 5}; function sThis(tp) { if(tp === global) {return "Global object";} else if(tp === obj_1) {return "Object obj_1";} else if(tp === obj_2) {return "Object obj_2";} else {return "Hmmm ... ";} } function func() { alert(this.a + ", " + sThis(this)); } func(); //3, Global object obj_1.f(); //4, Object obj_1 func.call(obj_2); //5, Object obj_2
  • 27. Conclusion ● I highly recommend reading Dmitry Soshnikov article on the global object, execution contexts, closures and this ● This article helped me understand these concepts and, as a result, improved my JavaScript skills in general ● I hope this presentation and the included Visio diagrams help you with your study of the above mentioned topics © Hans de Rooij hdr.is-a-geek.com