SlideShare a Scribd company logo
1 of 115
ECMA Script Javascript 6
By
Gaurav Khurana
Table of content
• History
• Goals of ECMAScript 6.
• What changes are incorporated in Javascript?
• Variables and Scoping.
– Block-Scoped Variables.
– Destructuring Objects.
• Extraction from Objects and Swap Variables
– Objects Literals.
– Multiple return Values.
– Destructuring Arrays.
– Destructuring Refutable by default.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
2
• Parameter Handling
– Parameter default Values.
– Rest parameters.
– Spread Operators.
– Named parameters.
• Arrow Functions
– Less to type.
– Lexical this no more that/self=this.
• Object Orientation and Modularity
– Object Literals
– Classes
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
3
– Subclass
– Modules :- named exports.
– Modules :- default export.
– Modules other features.
• Template String
– String interpolation
– Interpolation , raw string
– Regular Expression
– Other use cases
• Standard Library
– Map
– Sets
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
4
– Object.assign
– New String Methods
– New Array Methods
• Loops and Iteration
– Iterables and Iterators
– For of loop
• Generators
– Implementing an Iterator
– Asynchronous programming
• Symbols
– Enum -style value
– Property keys
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
5
• Various Other Features
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
6
Prerequisite
• Knowledge of ECMAScript 3 javascript is must
• ECMAScript 5 is a plus.
• Object Oriented Javascript advance
understanding.
• Understanding of nodejs would be plus (not
mandatory)
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
7
Lets get started
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
8
Background
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
9
Background continue. . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
10
Technical Committee
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
11
Important ES6 Terms
• TC39 (Ecma Technical Committee 39): the committee
evolving
JavaScript.
– Members: companies (all major browser vendors etc.).
– Meetings attended by employees and invited experts.
• ECMAScript: the official name of the language
– Versions: ECMAScript 5 is short for “ECMAScript Language
Specification, Edition 5”
• JavaScript:
– colloquially: the language
– formally: one implementation of ECMAScript
• ECMAScript Harmony: improvements after ECMAScript 5
(ECMAScript 6 and 7)
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
12
What changes are incorporated in
Javascript
• Variable and Scoping.
• Parameter Handling
• Arrow Functions
• Object Orientation and Modularity
• Template String
• Standard Library
• Loops and Iteration
• Symbols
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
13
Variable and scoping
• Typical problem with ES5
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
14
Block Scope
• Each Block has its own Lexical Enviorment.
• Let/const bind variables to the lexical
Enviorment.
• Variables declared with let/const are NOT
hoisted.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
15
var vs let
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
16
const
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
17
Question
• How will you create const in ECMA 5 ?
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
18
Block Scope
• Solution to this problem in ES5
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
19
Block Scope
• Solution to this problem in ES6
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
20
Block Scope
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
21
Block Function
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
22
Object Destructuring
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
23
Destructuring Array
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
24
Extraction from Objects and Swap
Variables
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
25
Destructuring Multiple Return Values
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
26
Destructuring refutable by default
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
27
Destructuring nested Objects
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
28
Destructuring nested Objects
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
29
Default Parameters
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
30
Arity
• Arity means number for parameters a function
can take.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
31
Rest Parameters
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
32
Rest Restriction
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
33
Arity
• Arity means number for parameters a function
can take
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
34
Spread Operators
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
35
Named Parameters
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
36
Arrow Function
• Sugar Syntax
• Lexical this
• No Constructor
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
37
Arrow Function elaborated
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
38
Understanding lexical this
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
39
Understanding lexical this . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
40
How Bind Would be working
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
41
Understanding lexical this . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
42
Answer to Const in ECMA5
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
43
Object Orientation and modularity
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
44
Object Orientation and modularity . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
45
Class old School way
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
46
Class ECMA5 way
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
47
Class ECMA 6 way
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
48
Module
• import
• export
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
49
Module . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
50
Template String
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
51
Template String use case
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
52
Template String for Dynamic RegExp
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
53
Map
• Key Value
• Maps can have non-string keys (Object can be
key)
• Maps don't have prototype leakage issues, i.e. no
need to
• use hasOwnProperty()
• Different Types of Values
• get(key)/ has(key) / set(key,val)
• clear()
• entries()
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
54
Map vs ECMA5 way
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
55
Map
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
56
Setting Map Different Ways
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
57
Object can be a Map
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
58
Iterating over Map
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
59
Filtering and Mapping
• You can map() and filter() arrays, but there are
no such operations for maps. The solution
1. Convert the map into an array of [key,value]
pairs.
2. Map or filter the array.
3. Convert the result back to a map.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
60
Filtering and Mapping . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
61
Set
• Set of Values (No Duplicates).
• Different Types of Values.
• add(key)/ has(key) / delete(key).
• entries() -> Iterator
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
62
Set . . .
Chrome
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
63
Question
Why I am not able to see methods of set2 or countries
object?
Chrome
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
64
Answer
• We are not able to see methods of set2 or
countries objects because they were made
enumerable : false
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
65
Answer . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
66
WeakMap
• Avoid memory leak
• Reference to the key object held weakly
• Key must be an object
• No iterator methods
• No clear
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
67
WeakMap not working in Traceur
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
68
WeakMap in Chrome harmony
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
69
Object Methods
• Object.setPrototypeOf(obj, proto)
• Object.assign(obj, mixin)
• Object.is(value1, value2)
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
70
Object property Assignment
ES 5 vs ES6
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
71
Object.setPrototypeOf
Fix this problem
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
72
Object.setPrototypeOf fixing problem
Now there will be another
problem .. Any guess?Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
73
Object.is
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
74
String Methods
• startsWith
• endsWith
• Includes
• repeat
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
75
Number Methods
• .isNAN() better than isNAN()
• .isFinite()
• .isInteger()
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
76
Number Methods . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
77
Number Methods . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
78
Math methods
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
79
Math Methods . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
80
Other Math methods
• log10, log2, log1p, expm1, cosh, sinh, tanh,
• acosh, asinh, atanh, hypot, trunc, sign
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
81
Array methods
• To be continued
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
82
Proxy
• Before we can get into what proxies are and
why they are useful, we first need to
understand what meta programming is.
• In programming we have two levels
– Base Level/application level (Code process user
input)
– Meta level(code process base level code).
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
83
Proxy . . .
• Let’s use JavaScript as both meta programming
language and base programming language.
• Classic example is eval in javascript
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
84
Proxy . . .
• Lets look at another example in pure javascript
• The program is examining its own structure while running. This doesn’t
look like meta programming, because the separation between
programming constructs and data structures is vague in JavaScript. All of
the Object.* methods can be considered meta programming functionality.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
85
Proxy . . .
• Different Types of Meta Programming
– Introspection:- You have read only access to the
structure of a program. Example (Object.keys())
– Self-Modification:- You can change that
structure.(Will show you example of this)
– Intercession:- You can redefine the semantics of
some language operations.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
86
Proxy . . .
• Self Modification Example
• It performs self-modification via the bracket operator
for property access, the assignment operator and
the delete operator
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
87
Proxy . . .
• JavaScript doesn’t currently support
intercession, proxies were created to fill that
gap.
• Operations we perform on Javascript objects
can be say
– Getting a property prop (obj.prop)
– Listing enumerable own properties (via
Object.keys(obj)).
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
88
Proxy . . .
• Proxies are special objects that allow you to
provide custom implementations for some of
these operations.
• A Proxy is created with two parameters
– Handler
• For each operation, there is a corresponding handler
method that – if present performs that operation.
• Such a method intercepts the operation (on its way to the
target) and is called a trap
– Target
• If the handler doesn’t intercept an operation then it is
performed on the target i.e. it acts as a fallback for handler.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
89
Proxy . . .
• Firefox support proxy there by following is the
example of proxy
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
90
Proxy . . .
• As I said if handler doesn’t intercept the
operation the operation is performed on
target.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
91
Proxy . . .
• As a matter of fact we made get request for
name in pervious example and for the same
we doesn’t have trap(or handler) assigned.
• Lets assign the same.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
92
Result
Proxy . . .
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
93
• Few more traps
For of loop
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
94
Generators
• Simple Example
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
95
Generators
• Passing Value Back to Generator
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
96
Generators
function* helloWorld(){
var next = yield "hello";
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
97
Generators
function* helloWorld(){
var next = yield "hello";
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
98
Generators
function* helloWorld(){
var next = yield "hello";
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
99
Generators
function* helloWorld(){
var next = yield "hello";
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
100
Generators
function* helloWorld(){
var next = yield "hello";
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
101
Generators
function* helloWorld(){
var next = yield ;
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
102
Generators
function* helloWorld(){
var next = yield
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
103
Generators
function* helloWorld(){
var next = yield
yield next;
}
var hw = helloWorld();
console.log(hw.next());
console.log(hw.next('world'));
console.log(hw.next());
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
104
Generators
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
105
Promise
• Before we jump into promise let me create a
scenario to explain the actual need of
promise.
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
106
Promise
• As we are familiar with the fact that java script
is single threaded.
• In browsers, JavaScript shares a thread with a
load of other stuff.
• What that stuff is differs from browser to
browser, but typically JavaScript is in the same
queue as painting, updating styles, and
handling user actions
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
107
Promise
• As a human being, you're multithreaded. You can
type with multiple fingers, you can drive and hold
a conversation at the same time.
• The only blocking function we have to deal with is
sneezing, where all current activity must be
suspended for the duration of the sneeze.
• That's pretty annoying, especially when you're
driving and trying to hold a conversation. You
don't want to write code that's sneezy
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
108
Promise
• You've probably used events and callbacks to get around
this. Here are events:
• Lets assume we need a intimation from browser when our
image is loaded.
• Lets Run this on browser
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
109
Promise
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
110
Promise
• What if the events happened before we
started listening for them
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
111
Promise
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
112
Promise
• Lets implement a Promise on Ajax call.
• I have created small http server using nodejs
which is listening to port 8888 and will return
text of file test.txt if requested url is /getData
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
113
Promise
• Additionally I have created on promisify-xml-
httpRequest.html
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
114
Prepared By Gaurav Khurana
(khurana.g@hotmail.com)
115

More Related Content

Viewers also liked

Die Kraft von Social Media
Die Kraft von Social MediaDie Kraft von Social Media
Die Kraft von Social MediaTWT
 
Mapa conceptual Gerencia de proyectos
Mapa conceptual Gerencia de proyectosMapa conceptual Gerencia de proyectos
Mapa conceptual Gerencia de proyectosaydebustamante
 
Encabezado y pie de pagina 2
Encabezado y pie de pagina 2Encabezado y pie de pagina 2
Encabezado y pie de pagina 2guiabuen
 
Chiquitinas%201[1]
Chiquitinas%201[1]Chiquitinas%201[1]
Chiquitinas%201[1]Aida Sanchez
 
Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...
Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...
Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...ranquiel
 
Clean Ganga Conference Post Show Report
Clean Ganga Conference Post Show ReportClean Ganga Conference Post Show Report
Clean Ganga Conference Post Show ReportVinay Kumar Bhati
 

Viewers also liked (17)

Time management sheikh jalal
Time management   sheikh jalalTime management   sheikh jalal
Time management sheikh jalal
 
Die Kraft von Social Media
Die Kraft von Social MediaDie Kraft von Social Media
Die Kraft von Social Media
 
Mapa conceptual Gerencia de proyectos
Mapa conceptual Gerencia de proyectosMapa conceptual Gerencia de proyectos
Mapa conceptual Gerencia de proyectos
 
Unidad 5
Unidad 5Unidad 5
Unidad 5
 
Práctica 12
Práctica 12Práctica 12
Práctica 12
 
Llibre
LlibreLlibre
Llibre
 
Encabezado y pie de pagina 2
Encabezado y pie de pagina 2Encabezado y pie de pagina 2
Encabezado y pie de pagina 2
 
Portefeuile vcc
Portefeuile vccPortefeuile vcc
Portefeuile vcc
 
Xiomara Rodriguez
Xiomara RodriguezXiomara Rodriguez
Xiomara Rodriguez
 
Chiquitinas%201[1]
Chiquitinas%201[1]Chiquitinas%201[1]
Chiquitinas%201[1]
 
Siie 2012 - Andorra
Siie 2012 - AndorraSiie 2012 - Andorra
Siie 2012 - Andorra
 
2 c
2 c2 c
2 c
 
Taller de robótica
Taller de robóticaTaller de robótica
Taller de robótica
 
3 a
3 a3 a
3 a
 
Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...
Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...
Para comprender los orígenes del pronunciamiento militar de 1943 hay que retr...
 
Philosophy of Education
Philosophy of EducationPhilosophy of Education
Philosophy of Education
 
Clean Ganga Conference Post Show Report
Clean Ganga Conference Post Show ReportClean Ganga Conference Post Show Report
Clean Ganga Conference Post Show Report
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
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
 
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
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
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
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
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
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
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
 
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
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
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
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
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...
 

Understanding ECMA Script 6 Javascript by Gaurav Khurana

  • 1. ECMA Script Javascript 6 By Gaurav Khurana
  • 2. Table of content • History • Goals of ECMAScript 6. • What changes are incorporated in Javascript? • Variables and Scoping. – Block-Scoped Variables. – Destructuring Objects. • Extraction from Objects and Swap Variables – Objects Literals. – Multiple return Values. – Destructuring Arrays. – Destructuring Refutable by default. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 2
  • 3. • Parameter Handling – Parameter default Values. – Rest parameters. – Spread Operators. – Named parameters. • Arrow Functions – Less to type. – Lexical this no more that/self=this. • Object Orientation and Modularity – Object Literals – Classes Prepared By Gaurav Khurana (khurana.g@hotmail.com) 3
  • 4. – Subclass – Modules :- named exports. – Modules :- default export. – Modules other features. • Template String – String interpolation – Interpolation , raw string – Regular Expression – Other use cases • Standard Library – Map – Sets Prepared By Gaurav Khurana (khurana.g@hotmail.com) 4
  • 5. – Object.assign – New String Methods – New Array Methods • Loops and Iteration – Iterables and Iterators – For of loop • Generators – Implementing an Iterator – Asynchronous programming • Symbols – Enum -style value – Property keys Prepared By Gaurav Khurana (khurana.g@hotmail.com) 5
  • 6. • Various Other Features Prepared By Gaurav Khurana (khurana.g@hotmail.com) 6
  • 7. Prerequisite • Knowledge of ECMAScript 3 javascript is must • ECMAScript 5 is a plus. • Object Oriented Javascript advance understanding. • Understanding of nodejs would be plus (not mandatory) Prepared By Gaurav Khurana (khurana.g@hotmail.com) 7
  • 8. Lets get started Prepared By Gaurav Khurana (khurana.g@hotmail.com) 8
  • 9. Background Prepared By Gaurav Khurana (khurana.g@hotmail.com) 9
  • 10. Background continue. . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 10
  • 11. Technical Committee Prepared By Gaurav Khurana (khurana.g@hotmail.com) 11
  • 12. Important ES6 Terms • TC39 (Ecma Technical Committee 39): the committee evolving JavaScript. – Members: companies (all major browser vendors etc.). – Meetings attended by employees and invited experts. • ECMAScript: the official name of the language – Versions: ECMAScript 5 is short for “ECMAScript Language Specification, Edition 5” • JavaScript: – colloquially: the language – formally: one implementation of ECMAScript • ECMAScript Harmony: improvements after ECMAScript 5 (ECMAScript 6 and 7) Prepared By Gaurav Khurana (khurana.g@hotmail.com) 12
  • 13. What changes are incorporated in Javascript • Variable and Scoping. • Parameter Handling • Arrow Functions • Object Orientation and Modularity • Template String • Standard Library • Loops and Iteration • Symbols Prepared By Gaurav Khurana (khurana.g@hotmail.com) 13
  • 14. Variable and scoping • Typical problem with ES5 Prepared By Gaurav Khurana (khurana.g@hotmail.com) 14
  • 15. Block Scope • Each Block has its own Lexical Enviorment. • Let/const bind variables to the lexical Enviorment. • Variables declared with let/const are NOT hoisted. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 15
  • 16. var vs let Prepared By Gaurav Khurana (khurana.g@hotmail.com) 16
  • 17. const Prepared By Gaurav Khurana (khurana.g@hotmail.com) 17
  • 18. Question • How will you create const in ECMA 5 ? Prepared By Gaurav Khurana (khurana.g@hotmail.com) 18
  • 19. Block Scope • Solution to this problem in ES5 Prepared By Gaurav Khurana (khurana.g@hotmail.com) 19
  • 20. Block Scope • Solution to this problem in ES6 Prepared By Gaurav Khurana (khurana.g@hotmail.com) 20
  • 21. Block Scope Prepared By Gaurav Khurana (khurana.g@hotmail.com) 21
  • 22. Block Function Prepared By Gaurav Khurana (khurana.g@hotmail.com) 22
  • 23. Object Destructuring Prepared By Gaurav Khurana (khurana.g@hotmail.com) 23
  • 24. Destructuring Array Prepared By Gaurav Khurana (khurana.g@hotmail.com) 24
  • 25. Extraction from Objects and Swap Variables Prepared By Gaurav Khurana (khurana.g@hotmail.com) 25
  • 26. Destructuring Multiple Return Values Prepared By Gaurav Khurana (khurana.g@hotmail.com) 26
  • 27. Destructuring refutable by default Prepared By Gaurav Khurana (khurana.g@hotmail.com) 27
  • 28. Destructuring nested Objects Prepared By Gaurav Khurana (khurana.g@hotmail.com) 28
  • 29. Destructuring nested Objects Prepared By Gaurav Khurana (khurana.g@hotmail.com) 29
  • 30. Default Parameters Prepared By Gaurav Khurana (khurana.g@hotmail.com) 30
  • 31. Arity • Arity means number for parameters a function can take. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 31
  • 32. Rest Parameters Prepared By Gaurav Khurana (khurana.g@hotmail.com) 32
  • 33. Rest Restriction Prepared By Gaurav Khurana (khurana.g@hotmail.com) 33
  • 34. Arity • Arity means number for parameters a function can take Prepared By Gaurav Khurana (khurana.g@hotmail.com) 34
  • 35. Spread Operators Prepared By Gaurav Khurana (khurana.g@hotmail.com) 35
  • 36. Named Parameters Prepared By Gaurav Khurana (khurana.g@hotmail.com) 36
  • 37. Arrow Function • Sugar Syntax • Lexical this • No Constructor Prepared By Gaurav Khurana (khurana.g@hotmail.com) 37
  • 38. Arrow Function elaborated Prepared By Gaurav Khurana (khurana.g@hotmail.com) 38
  • 39. Understanding lexical this Prepared By Gaurav Khurana (khurana.g@hotmail.com) 39
  • 40. Understanding lexical this . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 40
  • 41. How Bind Would be working Prepared By Gaurav Khurana (khurana.g@hotmail.com) 41
  • 42. Understanding lexical this . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 42
  • 43. Answer to Const in ECMA5 Prepared By Gaurav Khurana (khurana.g@hotmail.com) 43
  • 44. Object Orientation and modularity Prepared By Gaurav Khurana (khurana.g@hotmail.com) 44
  • 45. Object Orientation and modularity . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 45
  • 46. Class old School way Prepared By Gaurav Khurana (khurana.g@hotmail.com) 46
  • 47. Class ECMA5 way Prepared By Gaurav Khurana (khurana.g@hotmail.com) 47
  • 48. Class ECMA 6 way Prepared By Gaurav Khurana (khurana.g@hotmail.com) 48
  • 49. Module • import • export Prepared By Gaurav Khurana (khurana.g@hotmail.com) 49
  • 50. Module . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 50
  • 51. Template String Prepared By Gaurav Khurana (khurana.g@hotmail.com) 51
  • 52. Template String use case Prepared By Gaurav Khurana (khurana.g@hotmail.com) 52
  • 53. Template String for Dynamic RegExp Prepared By Gaurav Khurana (khurana.g@hotmail.com) 53
  • 54. Map • Key Value • Maps can have non-string keys (Object can be key) • Maps don't have prototype leakage issues, i.e. no need to • use hasOwnProperty() • Different Types of Values • get(key)/ has(key) / set(key,val) • clear() • entries() Prepared By Gaurav Khurana (khurana.g@hotmail.com) 54
  • 55. Map vs ECMA5 way Prepared By Gaurav Khurana (khurana.g@hotmail.com) 55
  • 56. Map Prepared By Gaurav Khurana (khurana.g@hotmail.com) 56
  • 57. Setting Map Different Ways Prepared By Gaurav Khurana (khurana.g@hotmail.com) 57
  • 58. Object can be a Map Prepared By Gaurav Khurana (khurana.g@hotmail.com) 58
  • 59. Iterating over Map Prepared By Gaurav Khurana (khurana.g@hotmail.com) 59
  • 60. Filtering and Mapping • You can map() and filter() arrays, but there are no such operations for maps. The solution 1. Convert the map into an array of [key,value] pairs. 2. Map or filter the array. 3. Convert the result back to a map. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 60
  • 61. Filtering and Mapping . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 61
  • 62. Set • Set of Values (No Duplicates). • Different Types of Values. • add(key)/ has(key) / delete(key). • entries() -> Iterator Prepared By Gaurav Khurana (khurana.g@hotmail.com) 62
  • 63. Set . . . Chrome Prepared By Gaurav Khurana (khurana.g@hotmail.com) 63
  • 64. Question Why I am not able to see methods of set2 or countries object? Chrome Prepared By Gaurav Khurana (khurana.g@hotmail.com) 64
  • 65. Answer • We are not able to see methods of set2 or countries objects because they were made enumerable : false Prepared By Gaurav Khurana (khurana.g@hotmail.com) 65
  • 66. Answer . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 66
  • 67. WeakMap • Avoid memory leak • Reference to the key object held weakly • Key must be an object • No iterator methods • No clear Prepared By Gaurav Khurana (khurana.g@hotmail.com) 67
  • 68. WeakMap not working in Traceur Prepared By Gaurav Khurana (khurana.g@hotmail.com) 68
  • 69. WeakMap in Chrome harmony Prepared By Gaurav Khurana (khurana.g@hotmail.com) 69
  • 70. Object Methods • Object.setPrototypeOf(obj, proto) • Object.assign(obj, mixin) • Object.is(value1, value2) Prepared By Gaurav Khurana (khurana.g@hotmail.com) 70
  • 71. Object property Assignment ES 5 vs ES6 Prepared By Gaurav Khurana (khurana.g@hotmail.com) 71
  • 72. Object.setPrototypeOf Fix this problem Prepared By Gaurav Khurana (khurana.g@hotmail.com) 72
  • 73. Object.setPrototypeOf fixing problem Now there will be another problem .. Any guess?Prepared By Gaurav Khurana (khurana.g@hotmail.com) 73
  • 74. Object.is Prepared By Gaurav Khurana (khurana.g@hotmail.com) 74
  • 75. String Methods • startsWith • endsWith • Includes • repeat Prepared By Gaurav Khurana (khurana.g@hotmail.com) 75
  • 76. Number Methods • .isNAN() better than isNAN() • .isFinite() • .isInteger() Prepared By Gaurav Khurana (khurana.g@hotmail.com) 76
  • 77. Number Methods . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 77
  • 78. Number Methods . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 78
  • 79. Math methods Prepared By Gaurav Khurana (khurana.g@hotmail.com) 79
  • 80. Math Methods . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 80
  • 81. Other Math methods • log10, log2, log1p, expm1, cosh, sinh, tanh, • acosh, asinh, atanh, hypot, trunc, sign Prepared By Gaurav Khurana (khurana.g@hotmail.com) 81
  • 82. Array methods • To be continued Prepared By Gaurav Khurana (khurana.g@hotmail.com) 82
  • 83. Proxy • Before we can get into what proxies are and why they are useful, we first need to understand what meta programming is. • In programming we have two levels – Base Level/application level (Code process user input) – Meta level(code process base level code). Prepared By Gaurav Khurana (khurana.g@hotmail.com) 83
  • 84. Proxy . . . • Let’s use JavaScript as both meta programming language and base programming language. • Classic example is eval in javascript Prepared By Gaurav Khurana (khurana.g@hotmail.com) 84
  • 85. Proxy . . . • Lets look at another example in pure javascript • The program is examining its own structure while running. This doesn’t look like meta programming, because the separation between programming constructs and data structures is vague in JavaScript. All of the Object.* methods can be considered meta programming functionality. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 85
  • 86. Proxy . . . • Different Types of Meta Programming – Introspection:- You have read only access to the structure of a program. Example (Object.keys()) – Self-Modification:- You can change that structure.(Will show you example of this) – Intercession:- You can redefine the semantics of some language operations. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 86
  • 87. Proxy . . . • Self Modification Example • It performs self-modification via the bracket operator for property access, the assignment operator and the delete operator Prepared By Gaurav Khurana (khurana.g@hotmail.com) 87
  • 88. Proxy . . . • JavaScript doesn’t currently support intercession, proxies were created to fill that gap. • Operations we perform on Javascript objects can be say – Getting a property prop (obj.prop) – Listing enumerable own properties (via Object.keys(obj)). Prepared By Gaurav Khurana (khurana.g@hotmail.com) 88
  • 89. Proxy . . . • Proxies are special objects that allow you to provide custom implementations for some of these operations. • A Proxy is created with two parameters – Handler • For each operation, there is a corresponding handler method that – if present performs that operation. • Such a method intercepts the operation (on its way to the target) and is called a trap – Target • If the handler doesn’t intercept an operation then it is performed on the target i.e. it acts as a fallback for handler. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 89
  • 90. Proxy . . . • Firefox support proxy there by following is the example of proxy Prepared By Gaurav Khurana (khurana.g@hotmail.com) 90
  • 91. Proxy . . . • As I said if handler doesn’t intercept the operation the operation is performed on target. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 91
  • 92. Proxy . . . • As a matter of fact we made get request for name in pervious example and for the same we doesn’t have trap(or handler) assigned. • Lets assign the same. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 92 Result
  • 93. Proxy . . . Prepared By Gaurav Khurana (khurana.g@hotmail.com) 93 • Few more traps
  • 94. For of loop Prepared By Gaurav Khurana (khurana.g@hotmail.com) 94
  • 95. Generators • Simple Example Prepared By Gaurav Khurana (khurana.g@hotmail.com) 95
  • 96. Generators • Passing Value Back to Generator Prepared By Gaurav Khurana (khurana.g@hotmail.com) 96
  • 97. Generators function* helloWorld(){ var next = yield "hello"; yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 97
  • 98. Generators function* helloWorld(){ var next = yield "hello"; yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 98
  • 99. Generators function* helloWorld(){ var next = yield "hello"; yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 99
  • 100. Generators function* helloWorld(){ var next = yield "hello"; yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 100
  • 101. Generators function* helloWorld(){ var next = yield "hello"; yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 101
  • 102. Generators function* helloWorld(){ var next = yield ; yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 102
  • 103. Generators function* helloWorld(){ var next = yield yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 103
  • 104. Generators function* helloWorld(){ var next = yield yield next; } var hw = helloWorld(); console.log(hw.next()); console.log(hw.next('world')); console.log(hw.next()); Prepared By Gaurav Khurana (khurana.g@hotmail.com) 104
  • 105. Generators Prepared By Gaurav Khurana (khurana.g@hotmail.com) 105
  • 106. Promise • Before we jump into promise let me create a scenario to explain the actual need of promise. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 106
  • 107. Promise • As we are familiar with the fact that java script is single threaded. • In browsers, JavaScript shares a thread with a load of other stuff. • What that stuff is differs from browser to browser, but typically JavaScript is in the same queue as painting, updating styles, and handling user actions Prepared By Gaurav Khurana (khurana.g@hotmail.com) 107
  • 108. Promise • As a human being, you're multithreaded. You can type with multiple fingers, you can drive and hold a conversation at the same time. • The only blocking function we have to deal with is sneezing, where all current activity must be suspended for the duration of the sneeze. • That's pretty annoying, especially when you're driving and trying to hold a conversation. You don't want to write code that's sneezy Prepared By Gaurav Khurana (khurana.g@hotmail.com) 108
  • 109. Promise • You've probably used events and callbacks to get around this. Here are events: • Lets assume we need a intimation from browser when our image is loaded. • Lets Run this on browser Prepared By Gaurav Khurana (khurana.g@hotmail.com) 109
  • 110. Promise Prepared By Gaurav Khurana (khurana.g@hotmail.com) 110
  • 111. Promise • What if the events happened before we started listening for them Prepared By Gaurav Khurana (khurana.g@hotmail.com) 111
  • 112. Promise Prepared By Gaurav Khurana (khurana.g@hotmail.com) 112
  • 113. Promise • Lets implement a Promise on Ajax call. • I have created small http server using nodejs which is listening to port 8888 and will return text of file test.txt if requested url is /getData Prepared By Gaurav Khurana (khurana.g@hotmail.com) 113
  • 114. Promise • Additionally I have created on promisify-xml- httpRequest.html Prepared By Gaurav Khurana (khurana.g@hotmail.com) 114
  • 115. Prepared By Gaurav Khurana (khurana.g@hotmail.com) 115