SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
>< nextprevious
Javascript Under The Hood
The Mysterious Parts
Tran Duc Thang
Framgia Vietnam - Business Strategy Office - Human Development Section
Demystifying Javascript’s “First-class functions”, “Scope”, “Closure”, and “this” keyword binding
1
>< nextprevious
“Javascript is the World's
Most Misunderstood
Programming Language!”
~ Douglas Crockford ~
2
>< nextprevious
3
Too much to remember, too weird to understand?
first-class functions
lexical scope
function scope
closure
IIFE
prototype
function statement
function expression
hoisting
this
function constructor
new
block scope
>< nextprevious
Some JS Quizes
4
http://jsbin.com/tojavun/edit?html,js,console,output
>< nextprevious
Some JS Quizes
5
http://jsbin.com/wabedaf/3/edit?html,js,output
Table of Contents
01
Basic Introductions
‣ First-class functions
‣ IIFE
‣ Hoisting
02
03
04
>< nextprevious
Scope
‣ Lexical Scope
‣ Function Scope vs Block Scope
Closure
‣ Understanding Closure
‣ Fixing the problems with Closure
this keyword binding
‣ Understanding this keyword
‣ Binding this
6
>< nextprevious
First-class functions
‣ Javascript value types
‣ string
‣ number
‣ boolean
‣ null
‣ undefined
‣ symbol (ES6)
‣ object
7
Primitive values
Object
>< nextprevious
First-class functions
‣ Everything which is not primitive is object.
‣ Function (and even Class in ES6) in Javascript is
actually object.
8
>< nextprevious
First-class functions
9
>< nextprevious
First-class functions
‣ First-class citizen (also type, object, entity, or
value) is an entity which supports all the
operations generally available to other entities.
‣ These operations typically include being
passed as an argument, returned from a
function, and assigned to a variable
10
>< nextprevious
First-class functions
‣ A programming language is said to have first-
class functions if it treats functions as first-
class citizens.
‣ Javascript has first-class functions!
11
>< nextprevious
First-class functions
12
>< nextprevious
First-class functions
‣ Function Statement
‣ Function Expression
13
>< nextprevious
First-class functions
14
Function Statement Function Expression
Defines function. Also known as
function declaration.
Defines a function as a part of a
larger expression syntax
Must begins with “function”
keyword
Must not begin with “function”
keyword
Must have a name
Can have a name or not (can be
anonymous)
>< nextprevious
First-class functions
15
>< nextprevious
First-class functions
‣ IIFE: Immediately Invoked Function Expression
is a Javascript function that runs as soon as it
is defined.
16
>< nextprevious
First-class functions
17
>< nextprevious
Hoisting
‣ Hoisting: The ability to use variable, function
before they are declared.
‣ Javascript only hoists declarations, not
initializations
18
>< nextprevious
Hoisting
19
>< nextprevious
Scope
‣ Scope is the set of variables, objects, and
functions you have access to
‣ 2 ways to create a Scope: Function and
Block*
20
>< nextprevious
Scope
‣ Lexical Scope vs Dynamic Scope
‣ Lexical Scope, or Static Scope: The scope of a
variable is defined by its location within the
source code and nested functions have access
to variables declared in their outer scope.
‣ Dynamic Scope: The scope of a variable
depends on where the functions and scopes are
called from
‣ Lexical Scope is write-time, whereas Dynamic
Scope is runtime
‣ Javascript has Lexical Scope!
21
>< nextprevious
Scope
22
‣ Global Scope
‣ Local Scope
‣ Nested Scope
‣ Outer Scope
‣ Inner Scope
‣ Function Scope
‣ Block Scope
>< nextprevious
Scope
23
IIFE can be used to
create a new scope!
>< nextprevious
Closure
‣ Closure is a function that can remember and
access its lexical scope even when it's
invoked outside its lexical scope
24
>< nextprevious
Closure
25
Unravel the problems
http://jsbin.com/wabedaf/3/edit?html,js,output
>< nextprevious
“this” keyword
‣ “this” does not refer to the function itself.
‣ “this” does not refer to the function’s lexical
scope.
‣ In most cases, the value of “this” is
determined by how a function is called.
‣ “this” may be different each time the function is
called.
26
>< nextprevious
“this” keyword
‣ “this” does not refer to the function itself.
27
>< nextprevious
“this” keyword
‣ Default binding: Standalone function invocation.
“this” is bind to global object (in non-strict mode)
28
>< nextprevious
“this” keyword
‣ Implicit binding:
Function is invoked from a
containing object. “this” is
bind to the containing
object.
29
>< nextprevious
“this” keyword
‣ Explicit binding:
Function is called with
call, apply or bind
method. “this” is bind to
the object passed to the
binding method.
30
>< nextprevious
“this” keyword
‣ new keyword binding: “this” is bind to the new
object that is created
31
>< nextprevious
“this” keyword
‣ Arrow function: “this” is lexically adopted from the
enclosing scope
32
>< nextprevious
“this” keyword
‣ Binding priority
‣ Arrow function
‣ new keyword
‣ Explicit binding, by using call, apply and bind
‣ Implicit binding
‣ Default binding
33
>< nextprevious
Some best practices
‣ Try to avoid Global variables
‣ Always declare variables
‣ Put variables declaration on top
34
Use Strict Mode
>< nextprevious
References
‣ You don’t know JS (https://github.com/getify/You-
Dont-Know-JS)
‣ Speaking JS (http://speakingjs.com/)
‣ Exploring ES6 (http://exploringjs.com/es6/)
‣ http://www.2ality.com/ JavaScript and more
‣ Mozilla Developer Network (https://
developer.mozilla.org/en-US/docs/Web/
JavaScript)
‣ Tìm hiểu về Strict Mode trong Javascript (https://
viblo.asia/thangtd90/posts/jaqG0QQevEKw)
35
>< nextprevious
Thank you for listening!
Q&A
For any discussion, you can refer this post on Viblo
https://viblo.asia/thangtd90/posts/WApGx3P3M06y
36

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
9. ES6 | Let And Const | TypeScript | JavaScript
9. ES6 | Let And Const | TypeScript | JavaScript9. ES6 | Let And Const | TypeScript | JavaScript
9. ES6 | Let And Const | TypeScript | JavaScript
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
 
Callback Function
Callback FunctionCallback Function
Callback Function
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
 

Ähnlich wie Javascript under the hood 1

The Spring Framework: A brief introduction to Inversion of Control
The Spring Framework:A brief introduction toInversion of ControlThe Spring Framework:A brief introduction toInversion of Control
The Spring Framework: A brief introduction to Inversion of Control
VisualBee.com
 
Automated User Tests with Apache Flex
Automated User Tests with Apache FlexAutomated User Tests with Apache Flex
Automated User Tests with Apache Flex
Gert Poppe
 

Ähnlich wie Javascript under the hood 1 (20)

Javascript internals
Javascript internalsJavascript internals
Javascript internals
 
A new execution model for Nashorn in Java 9
A new execution model for Nashorn in Java 9A new execution model for Nashorn in Java 9
A new execution model for Nashorn in Java 9
 
Event Programming JavaScript
Event Programming JavaScriptEvent Programming JavaScript
Event Programming JavaScript
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32
 
javascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptjavascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.ppt
 
Javascript
JavascriptJavascript
Javascript
 
Nodejs from zero to hero
Nodejs from zero to heroNodejs from zero to hero
Nodejs from zero to hero
 
Ditching jQuery Madison
Ditching jQuery MadisonDitching jQuery Madison
Ditching jQuery Madison
 
How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
 
Dynamic Actions, the Hard Parts
Dynamic Actions, the Hard PartsDynamic Actions, the Hard Parts
Dynamic Actions, the Hard Parts
 
WEB MODULE 3.pdf
WEB MODULE 3.pdfWEB MODULE 3.pdf
WEB MODULE 3.pdf
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Java script
Java scriptJava script
Java script
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
 
Automated User Tests with Apache Flex
Automated User Tests with Apache FlexAutomated User Tests with Apache Flex
Automated User Tests with Apache Flex
 
Java script
Java scriptJava script
Java script
 
The Spring Framework: A brief introduction to Inversion of Control
The Spring Framework:A brief introduction toInversion of ControlThe Spring Framework:A brief introduction toInversion of Control
The Spring Framework: A brief introduction to Inversion of Control
 
Automated User Tests with Apache Flex
Automated User Tests with Apache FlexAutomated User Tests with Apache Flex
Automated User Tests with Apache Flex
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

Javascript under the hood 1