Chloe Annable JavaScript Mastery - A Step by Step Beginner - S Guide To Learning JavaScript Programming
Chloe Annable JavaScript Mastery - A Step by Step Beginner - S Guide To Learning JavaScript Programming
Introduction
Chapter 1: What is JavaScript
for loops
Chapter 4: Functions
Chapter 5: Data Structures: Objects and Arrays
Conclusion
© ACopyright A2019 A– AChloe AAnnabler
The Acontent Acontained Awithin Athis Abook Amay Anot Abe Areproduced, Aduplicated, Aor Atransmitted
without Adirect Awritten Apermission Afrom Athe Aauthor Aor Athe Apublisher.
A
Under Ano Acircumstances Awill Aany Ablame Aor Alegal Aresponsibility Abe Aheld Aagainst Athe
publisher, Aor Aauthor,
A for
A Aany damages,
A Areparation, or
A monetary
A loss
A due
A to
A the
A
Legal ANotice:
This Abook Ais Acopyright Aprotected. AIt Ais Aonly Afor Apersonal Ause. AYou Acannot Aamend,
distribute, Asell, Ause, Aquote Aor Aparaphrase Aany Apart, Aor Athe Acontent Awithin Athis Abook,
A
Disclaimer ANotice:
Please Anote Athe Ainformation Acontained Awithin Athis Adocument Ais Afor Aeducational Aand
entertainment Apurposes Aonly. AAll Aeffort Ahas Abeen Aexecuted Ato Apresent Aaccurate, Aup Ato
A
date, Areliable, Acomplete Ainformation. ANo Awarranties Aof Aany Akind Aare Adeclared Aor Aimplied.
A
Readers Aacknowledge Athat Athe Aauthor Ado Anot Aengage Ain Athe Arendering Aof Alegal, Afinancial,
A
medical, Aor Aprofessional Aadvice. AThe Acontent Awithin Athis Abook Ahas Abeen Aderived Afrom
A
various Asources. APlease Aconsult Aa Alicensed Aprofessional Abefore Aattempting Aany Atechniques
A
By Areading Athis Adocument, Athe Areader Aagrees Athat Aunder Ano Acircumstances Ais Athe Aauthor
responsible Afor Aany Alosses, Adirect Aor Aindirect, Athat Aare Aincurred Aas Aa Aresult Aof Athe Ause
A
of Athe Ainformation Acontained Awithin Athis Adocument, Aincluding, Abut Anot Alimited Ato, Aerrors,
A
A
Introduction
JavaScript Ais Aa Adistinguished Aand Adeciphered Aprogramming Alanguage
Athat Aadheres Ato Athe AECMAScript Aspecification. AThe Aprogramming
AAccompanied Aby AHTML Aand ACSS, AJavaScript Ais Aone Aof Athe
Afacilitates Ainterchangeable Aweb Apages Aand Ais Aa Avital Apart Aof Aweb
Asignificant Aweb Abrowsers Aconsist Aof Athe AJavaScript Aengine Ato Afunction
Acorrectly. ABrendan AEich Adeveloped AJavaScript Ain A1995 Awhen Ahe Awas
Aon Athe Aclient-side Aby Athe Auser. AYou Acan Ainsert AJavaScript Aengines
Ainto Adifferent Atypes Aof Ahost Asoftware, Awhich Aincludes Adatabases Aand
The Aconcepts Aof AJavaScript Aare Aexplained Afurther Ain Athis Abook Awith
Athe Agoal Ato Ahelp Ayou Alearn Aand Aunderstand AJavaScript Alanguage
Abrowser, Aand ACSS Aare Aall Athat's Aneeded Ato Alearn AJavaScript. ATo Aget
Athe Alearning Aprocess Astarted, Aone Aof Athe Aessential Atools Ais Aa Atext
Aeditor, Aand Ait Ais Arequired Ato Awrite Acodes. AYou’ll Aalso Aneed Aa Abrowser
Ato Aunveil Ayour Adeveloped Aweb Apages. AThere Aare Adifferent Atypes Aof
Atext Aeditors Asuch Aas Athe ASublime AText, ANotepad++ Aand Abrowsers
A
Chapter 1: What is JavaScript
JavaScript, Aabbreviated AJS, Ais Aa Ahigh-level Aprogramming Alanguage
Aintroduced Ato Aadd Aspecific Aprograms Ato Aweb Apages, Aand Ait Ahas Abeen
Aadopted Aby Aall Amajor Aweb Abrowsers. AJavaScript Acan Abe Aused Ato Abuild
Adifferent Aforms Aof Aactivities Awithin Aweb Apages Aand Ais Aone Aof Athe
Aessential Acomponents Aof Athe AWorld AWide AWeb A(www), Awhich Aare
ASheets). AJavaScript Aand AHTML Aare Aused Ato Adevelop Aweb Apages.
AJavaScript Abrings Aa Apage Ato Alife Aby Aadding Aspecial Aeffects Asuch Aas
Client-Side JavaScript
A
Client-Side AJavaScript Ais Athe Ausual Aform Aof AJavaScript Alanguage. AThe
Ascript Amust Abe Ainserted Aor Areferenced Aby Aan AHTML Adocument Aso
Athat Athe Abrowser Acan Ainterpret Athe Acode. AIt Aenables Aweb Apages Ato
Awhen Athey Awant Ato Asubmit Aforms Aand Aalso Adetermine Aif Aall Aentries
Advantages of JavaScript
A A
· AWith AJavaScript, Ausers Acan Aorganize Ainput Abefore Agetting Athe Apage
Asent Ato Athe Aserver, Awhich Aautomatically Areduces Aloads Aon Athe Aserver.
· AJavaScript Aenables Aswift Aresponse Ato Apage Avisitors. AThe Apage Adoes
Anot Ahave Ato Areload Abefore Avisitors Acan Asee Aif Athere Awas Aan Aerror Ain
Atyping.
· AFor Asecurity Areasons, Aclient-side AJavaScript Adoes Anot Aread Aor Awrite
Afiles
Aetc.
Javascript Placement
A
Athis:
<html>
<head>
<script Atype="text/JavaScript">
<!--
function AsayHi() A{
alert("Hello AWorld")
}
//-->
</script>
</head>
<body>
Tap Ahere Afor Aresult
<input Atype="button" Aonclick="sayHi()" Avalue="Say AHi" A/>
</body>
</html>
JavaScript Ain A<body>...</body> ASection
Sometimes Ayou Awant Aa Ascript Ato Arun Aimmediately Aand Ato Acreate Ascript
Aon Athe Acontent Apage, Ainsert Athe Ascript Awithin Athe A<body> Asection.
<html>
<head>
</head>
<body>
<script Atype="text/JavaScript">
<!--
document.write("Hello AWorld")
//-->
</script>
<p>This Ais Aweb Apage Abody A</p>
</body>
</html>
JavaScript Ain A<body> Aand A<head> ASections
Insert Ayour AJavaScript Acode Ain A<head> Aand A<body> Asection Alike Athis:
<html>
<head>
<script Atype="text/JavaScript">
<!--
function AsayHi() A{
alert("Hello AWorld")
}
//-->
</script>
</head>
<body>
<script Atype="text/JavaScript">
<!--
document.write("Hello AWorld")
//-->
</script>
<input Atype="button" Aonclick="sayHi()" Avalue="Say AHi" A/>
</body>
</html>
Acodes Aand Athen Aintegrate Athe Aexternal Afile Ainto Athe AHTML Afiles. AThe
Asample Abelow Adisplays Ahow Ato Aintegrate Aan Aexternal AJavaScript Afile
Awithin Athe AHTML Acode Autilizing Athe Ascript Atag Aand Asrc Aattribute.
<html>
<head>
<script Atype="text/javascript" Asrc="filename.js" A></script>
</head>
<body>
.......
</body>
</html>
The Aexternal Afile Asource Afile Ashould Abe Asaved Awith Aan Aextension A.js.
Summary
In Athis Afirst Achapter, Awe Aexplained Athe Aorigination Aof AJavaScript Aand
Aits Aplacement Ainto Ainternal Aand Aexternal Afiles. AWe Adiscussed Athe Ause
Exercise
How Ado Ayou Ainsert AJavaScript Acode Ainto Athe Ahead, Abody Aand Ainclude
Aexternal Afile Ascript Ain Aan AHTML Adocument?
Solution
The AScript Afor Athe Ahead Apart Ais A<head> A</head>.
The AScript Afor Athe Abody Apart Ais A<body> A</body>.
The AScript Awithin Athe Abody Ais A<body> A</body> Aand A<head> A</head>.
The AScript Ato AInclude Aexternal Afile AScript Ain Athe A<head>...</head>.
Chapter 2: Values, Types, and Operators
In Athe Aworld Aof Acomputers, Athere Ais Adata. AYou Acan Acreate Anew Adata,
Aread Adata Aand Achange Adata, Awhich Aare Aall Astored Aas Aa Alook-alike Along
Asuccession Aof Abits. ADefine Abits Aas Azeros Aand Aones Athat Atake Aa Astrong
Aor Aweak Asignal, Aand Ahigh Aor Alow Aelectrical Acharge Afrom Ainside Athe
Acomputer. AAll Adata Aand Apieces Aof Ainformation Aare Adescribed Aas Aa
Values
Take Aa Adeep Abreath Aand Athink Aof Aan Aocean Aof Abits. AThe Alatest APCs
Acontains Amore Athan A30 Abillion Abits Ain Aits Adata Astorage. AWe Ause Abits
Ato Acreate Avalues. AThe Acomputer Acan Afunction Acorrectly Abecause Aevery
Abit Aof Ainformation Ais Asplit Ainto Avalues. AEvery Avalue Aconsists Aof Aa
Atype Athat Ainfluences Aits Arole, Aand Avalues Acan Abe Anumbers, Atext Aor
Afunctions, Aetc. ATo Agenerate Avalue, Ayou Aneed Ato Ainvoke Aits Aname, Aand
Arithmetic
Arithmetic Ais Athe Amajor Athing Ato Ado Awith Anumbers. AThe Amultiplication,
Aaddition, Aand Asubtraction Aof Amore Athan Aone Anumber Ato Aproduce
Aanother Anumber Ais Aan Aarithmetic Aoperation. AThis Ais Aan Aexample Aof
100 A+ A4 A* A11
The A+ Aand A* Asymbols Aare Acalled Aoperators, Athe Afirst Ameans Aaddition
Awhile Athe Aother Ameans Amultiplication. AAn Aoperator Ainserted Abetween
Atwo Avalues Awill Aproduce Aanother Avalue. AThe A- Aoperator Ais Afor
Asubtraction Aand Athe A/ Aoperator Ais Afor Athe Adivision. AIf Aoperators Ashow
Athe Away Athey Aare Aapplied. AIf Aseveral Aoperators Awith Athe Asame
Aprecedence Ashow Aright Anext Ato Aeach Aother Alike A1 A- A2 A+ A1, Aapply
Special Anumbers
JavaScript Aconsists Aof Athree Aunique Avalues Athat Ado Anot Aact Alike
Anumbers Abut Aare Aregarded Aas Anumbers. AInfinity Aand A-Infinity Aare Athe
Afirst Atwo, Awhich Amean Athe Apositive Aand Anegative Ainfinities, Aand Athe
Alast Avalue Ais Athe ANaN. ANaN Asays A“not Aa Anumber,” Aalthough Ait Ais Aa
Strings
A Astring Ais Athe Asuccession Aof Anumbers. AThe Astring Ais Athe Anext Adata
Atype, Aand Athey Arepresent Atext. AStrings Aconfine Atheir Acontent Ain Aquotes.
Astrings Amatch. AThe Aelements Awithin Athe Aquotes Acreate Aa Astring Avalue
AJapanese, Aetc. AYou Acannot Asubtract, Amultiply, Aor Adivide Astrings Abut
Ayou Acan Ause Athe A+ Aoperator, Awhich Awill Anot Aadd Abut Aconcatenates
Unary operators
A
Symbols Ado Anot Arepresent Aall Athe Aoperators. AYou Acan Awrite Asome
Aoperators Ain Awords. AA Aclear Aexample Ais Aa Atype Aof Aoperator, Aand Athis
Aoperator Acreates Aa Astring Avalue Awith Athe Aname Aof Athe Atypeof Aits
Aattached Avalue.
console.log(typeof A4.5)
// A→ Anumber
console.log(typeof A"x")
// A→ Astring
The Asecond Adisplayed Aoperator Ais Acalled Athe Abinary Aoperator Abecause
Athey Ause Atwo Avalues, Awhile Aoperators Athat Ause Aone Avalue Aare Athe
Aunary Aoperator.
Afalse.
Comparison
Comparison Ais Aa Away Ato Acreate ABoolean Avalues:
console.log(3 A> A2)
// A→ Atrue
console.log(3 A< A2)
// A→ Afalse
The A> Aand A< Acharacters Aare Athe Asigns Athat Arepresent A“is Agreater Athan”
Aand A“is Aless Athan,” Aaccordingly. AYou Acan Ause Abinary Aoperators Ain Aa
ABoolean Avalue Athat Adetermines Aif Athe Acontained Avalue Aif Atrue Aor Afalse.
Acharacters A(! A-, Aand Aso Aon) Aare Aalso Apresent Ain Athe Aordering. AWhen
AJavaScript Awant Ato Acompare Astrings, Ait Arecognizes Acharacters Afrom Aleft
Here Aare Aother Arelated Aoperators A<= A(less Athan Aor Aequal Ato), A>=
A(greater Athan Aor Aequal Ato), A== A(equal Ato), Aand!= A(not Aequal Ato).
Asignifies Alogical Aor Aand Aits Aresults Adepend Aon Awhere Athe Avalues
Ato Atrue.
The A|| Aconsists Aof Athe Alowest Aprecedence Aof Aall Aoperators, Athen A&&,
Athe Acomparison Aoperators A(>, A==, Aetc.), Aand Aso Aon. AThe Aexample
console.log(8 A* Anull)
// A→ A0
console.log("5" A- A1)
// A→ A4
console.log("5" A+ A1)
// A→ A51
console.log("five" A* A2)
// A→ ANaN
console.log(false A== A0)
// A→ Atrue
When Ayou Aassign Aan Aoperator Athe Awrong Avalue, AJavaScript Asilently
Areturns Athat Avalue Ato Athe Aexact Atype Ait Arequires Ausing Athe Atype
Acoercion Arule. AIn Athe Afirst Aexpression, Athe Anull Aturns Ato A0, Aand Athe A5
Ain Athe Asecond Aremains A5 A(from Astring Ato Anumber). AIn Athe Athird
AWhen Aodd Anumbers Asuch Aas A"five" Aor Aundefined Achanges Ato Athe
Anumber, Ait Agets Athe Avalue Aof ANaN. AIf Ayou Awant Ato Adifferentiate
Abetween Avalues Aof Athe Asame Atype Ausing A==, Athe Aoutput Ashould Abe
Atrue Aif Athe Avalues Aare Asimilar Aexcept ANaN. AIf Ayou Awant Ato Atest Aif Aa
Avalue Acontains Aa Areal Avalue, Ause Athe A== A(or! A=) Aoperator Ato Acompare
Acomparison Aoperators.
Avalues Acontained Ain Athe Aleft Aside Ato ABoolean Atype Ato Adecide, Aalthough
Ait Adepends Aon Athe Aoperators Aand Athe Atype Aof Agenerated Aresult, Abut
Awill Aalways Areinstate Athe Aleft Aor Aright-hand Avalue. AThe A|| Aoperator
Asends Aback Avalue Ato Athe Aleft Awhen Ait Acan Abe Achanged Ato Atrue Aand
ABoolean Avalue Aconversion Arules Aindicate Athat A0, ANaN, Aand Aempty
Astring A(") Acount Aas Afalse Awhile Aother Avalues Aare Atrue. ATherefore A0 A|| A-
1 Aoutputs A-1, Aand A"" A|| A"!?" Ayields A"!?". AThe A&& Aoperator Aoperates
Aidentically Abut Athe Aother Away Aaround. AWhen Avalues Ato Athe Aleft Acan
Abe Achanged Ato Afalse, Areturn Athe Avalue, Aor Ait Asends Athe Avalue Ato Athe
Aright. ABoth Aoperators Aevaluate Athe Avalue Ato Athe Aright Aonly Awhen Ait Ais
Arequired. AFor Aexample, Awe Ahave Athe Afollowing Avalues Aset Aas Atrue A||
AX, Athe Avalue Aof AX Awill Abe Atrue Aand Awill Anot Aconsider Ait. AThe Asame
Arule Aapplies Ato Athe Afalse A&& AX, Awhich Athe Ax Ais Afalse Aand Awill
Aoverlook Ait. AYou Acan Acall Athis Aprocess Athe Ashort-circuit Aevaluation.
Summary
This Achapter Alooks Aat Athe Afour Atypes Aof AJavaScript Avalues, Awhich Aare
Astrings, Anumbers, Aundefined Avalues Aand ABooleans. AThese Avalues Aare
Adeveloped Aby Ainserting Atheir Anames Aas Atrue, Anull Aor Avalue A(13,
A"ABC"). AOperators Acan Aintegrate Aand Achange Avalues. AWe Alooked Aat
Abinary Aoperators Afor Aarithmetic A(+, A-, A*, A/, Aand A%), Astring
Aconcatenation A(+), Acomparison A(==, A!=, A===, A!==, A<, A>, A<=, A>=), Aand
Alogic A(&&, A||), Aand Aalso Avarious Aunary Aoperators A(- Ato Anullify Aa
Anumber, A! Ato Anullify Alogically, Aand Atype Aof Ato Asearch Afor Aa Avalue’s
Atype) Aand Aa Aternary Aoperator A(?:) Ato Achoose Aone Aof Atwo Avalues
Adepending Aon Aa Athird Avalue. AYou Awill Aget Asufficient Ainformation Ato
Ause AJavaScript Alike Aa Asmall Acalculator, Aand Ayou Awill Aimprove Ain Athe
Afollowing Achapters.
Exercise
Write Aa AJavaScript Apractice Ato Abuild Aa Avariable Athrough A
a Auser-defined Aname.
Solution
Chapter 3: Program Structure
We Awill Abegin Athe Aexpansion Aof Athe AJavaScript Aprogramming Alanguage
Acommands Abeyond Asentence Afragments Aand Anouns.
Aan Aexpression. AAn Aexpression Awithin Aa Abinary Aoperator Aset Ato Atwo
Ahuman Alanguage Atranslates. AIt Aenables Aus Ato Adevelop Aexpressions Athat
Acomplete Athe Asentence. AA Alist Aof Astatements Ais Acalled Aa Aprogram, Aand
Athe Amost Aaccessible Atype Aof Astatement Ais Aa Aline Aof Acode Awith Aa
For Ainstance:
1;
!false;
With Athis Afunction, Aa Astatement Acan Astand Aindependently, Aand Ait Acan
Aadd Aa Afeature Athat Achanges Acolors Aoccasionally Ato Aa Ascreen Aor Amodify
Athe Ainner Astate Aof Athe Amachine Ainfluencing Athe Afollowing Astatements
Aafter Ait.
Bindings
JavaScript Auses Abinding Aof Avariables Ato Ahold Avalues, Afor Ainstance:
Get Amoney A= A5 A* A5;
Get Ais Athe Akeyword Ain Athat Astatement, Aand Ait Ameans Athat Athe Asentence
Awill Aspecify Aa Abinding Atogether Awith Athe Abinding Aname Aand Avalue Acan
Abe Aattached Ausing Athe A= Aoperator Aand Aan Aexpression. AUse Aa Adefined
Abinding Aas Aan Aexpression, Aan Aexpression Acontaining Athe Abinding Avalue.
Aused Ato Adeclare Aa Aname Ato Aa Avalue Ato Aenable Aeasy Areference Alater Aon.
Binding Anames
Although Athere Aare Asome Areserved Awords Alike Aconst, Aclass, Adefault,
Abreak, Acontinue, Adelete, Ado, Aelse Ayet Aany Aword Acan Abe Acalled Aa
Abinding Aname, Ait Acan Aalso Aconsist Aof Adigits Abut Ado Anot Abegin Athe
Asigns A($) Aor Aunderscores A(_) Abut Ado Anot Aentertain Aany Aother Aspecial
The Aenvironment
The Agroup Aof Abinding Aand Avalues Aexisting Aat Aa Astipulated Atime Ais
Aknown Aas Athe Aenvironment. AWhen Ayou Alaunch Aa Aprogram, Athe
Alaunched Awebsite Aas Awell Aas Aread Athe Akeyboard Aand Amouse Ainput.
Functions
A Abit Aof Aprogram Aenclosed Ain Avalue Ais Acalled Aa Afunction, Aand Athese
Avalues Aare Aadded Ato Alaunch Athe Awrapped Aprogram. AFor Ainstance, Aa
Aparenthesis Avalue Ato Athe Aprogram Awithin Athe Afunction. ADefine Avalues
Aarguments Ato Athe Adevice Athat Aoutputs Athe Atext. AIn Amodern Abrowsers,
Athe Aoutput Ais Aoften Ain Athe AJavaScript Aconsole Athat Ais Ainvisible Aby
Adefault; Ayou Acan Atap Athe AF12 Acommand Aon Ayour Akeyboard Aor Athe
Aperiod Acharacters, Abut Aconsole.log Adoes Abecause Ait Ais Aan Aexpression
Athat Areclaims Athe Alog Aproperty Afrom Athe Aconsole Abinding Avalue.
Return Avalues
Functions Aare Aused Ato Aproduce Aside Aeffects. AThey Acan Aalso Aprovide
Avalues Athat Ado Anot Aneed Aside Aeffects. AFor Ainstance, Athe Afunction
ACalculate.max Awill Atake Aa Asum Aof Anumber Aarguments Aand Areturns Athe
Agreatest.
console.log(Calculate.max(2, A4));
// A→ A4
JavaScript Aregards Aanything Athat Aprovides Avalue Aas Aan Aexpression,
Awhich Aenables Afunction Acalls Ato Aattach Ainto Athe Asubstantial Aexpression.
ACalculate.max:
Aof Aprogram Aconsists Aof Atwo Atypes Aof Astatement; Athe Afirst Ademand
Anumbers Afrom Athe Auser Awhile Athe Asecond Adisplays Athe Asquare Aof Athe
A Avalue Ais Achanged Ato Aa Anumber Aby Athe Afunction Anumber Aand Aoutputs
Aa Astring Avalue.
Conditional Aexecution
The Akeyboard Ais Aused Ato Adevelop Aconditional Aexecutions Ain AJavaScript.
AYou Amay Awant Asome Acode Ato Aexecute Aif, Aand Aonly Aif, Aa Aspecific
Acondition Ais Apositive. ALet Aus Adisplay Athe Asquare Aof Athe Ainput Aif Aonly
Aaccomplish.
The ANumber.isNaN Afunction Ais Aa Ahigh-level AJavaScript Afunction Athat
Aoutputs Aonly Atrue Aif Athe Astatement Ais Adeclared Aas ANaN. AThe Anumber
Afunction Areturns ANaN Awhen Aan Aassigned Astring Ais Anot Aa Avalid Anumber.
AStatements Aafter Athe Aif Astatement Ais Aenclosed Ain Abraces A({and}).
ABraces Aare Aused Ato Acategorize Adifferent Anumbers Aof Astatement Awithin
28
if A(1 A+ A1 A== A2) Aconsole.log("It's Atrue");
// A→ AIt's Atrue
The Aelse Akeyword Acan Abe Autilized Atogether Awith Athe Aif Astatement Ato
Aprovide Atwo Adifferent Aexecution Apaths.
Agreater Athan A10, Ait Aselects Athe Aelse Abranch Awhich Aconsists Aof Aa
This Ais Athe Away Ato Awrite Aa Aprogram Athat Adisplays Aall Athe Aeven
Anumbers Afrom A0 Ato A12.
console.log(0);
console.log(2);
console.log(4);
console.log(6);
console.log(8);
console.log(10);
console.log(12);
A Away Ato Arun Aa Aclock Aof Acode Amultiple Atimes Ais Adescribed Aas Aa Aloop.
The Alooping Acontrol Aflow Aenables Athe Auser Ato Areturn Ato Acertain Apoints
Ain Awritten Aprograms Aand Aredo Ait Awith Athe Acurrent Astate Aof Athe
Ain Aparentheses Afollowed Aby Aa Astatement Alike Athe Aif Astatement. AThe
Astates Atrue Awhen Atranslated Ato ABoolean. AEach Atime Athe Aloop Arepeats,
Anumbers Aadopt Aa Anumber Atwice Aof Atheir Aprevious Avalue. ALet Aus Awrite
Aa Aprogram Athat Aevaluates Aand Adisplay Athe Avalue Aof A210 A(2 Araised Ato
Athe Apower Aof A10th). AWe Awill Autilize Atwo Abindings. AOne Ato Akeep Aan
Aeye Aon Aour Aresult Aand Athe Aother Ato Acalculate Ahow Amany Atimes Athe
Avalue Aof Atwo Amultiplied Aby Athe Aresult. AIt Amultiplies Auntil Athe Asecond
Abegins Ato Acheck Aif Ait Aends Aafter Athe Afirst Aexecution Aonly. AFollow Athe
Afollowing Asteps:
let AyourName;
do A{
yourName A= Aprompt("Who Aare Ayou?");
} Awhile A(! AyourName);
console.log(yourName);
A
A
for loops
A Alot Aof Aloops Afollow Athe Awhile Aloop Apattern, Acreating Aa Acounter
Abinding Ato Atrack Athe Aloop's Aprogress. AThen Aa Awhile Aloop Awith Aa Atest
Aexpression Ato Asee Aif Athe Acounter Areaches Athe Aset Avalue.
for A(let Anumber A= A0; Anumber A<= A10; Anumber A= Anumber A+ A4) A{
console.log(number);
}
// A→ A0
// A→ A4
// Aetc
The Aparentheses Amust Aconsist Aof Atwo Asemicolons Aafter Athe Akeyword,
Athe Afirst Apart Athat Ais Abefore Athe Afirst Asemicolon Aseparates Athe Aloop Aby
Adescribing Aa Abinding. AThe Aother Apart Ais Athe Astatement Athat Adetermines
Aif Athe Aloop Ashould Acontinue. AHere Ais Athe Acode Athat Aevaluate A210
Aconfined Aloop. AThe Abreak Astatement Aevaluates Athis Aprogram Aif Athe
Afirst Anumber, Awhich Ais Aboth Agreater Athan Aor Aequal Ato A20, Aand Acan Abe
Azero. AThe Afor Ain Athe Aexample Ais Anot Achecked Aat Athe Aend Aof Athe Aloop,
Awhich Ameans Auntil Athe Abreak Astatement Ainside Ais Aimplemented Athe
Athat Acan Abe Autilized Awhen Abinding Anames Awith Adifferent Awords:
fuzzylittleturtle
fuzzy_little_turtle
FuzzyLittleTurtle
fuzzyLittleTurtle
Comments
Sometimes Araw Acodes Ado Anot Atransmit Aevery Asingle Apiece Aof
Ainformation Athat Ayou Awant Athe Aprogram Ato Asend Ato Areaders Aor Aspreads
Athe Amessage Ain Aa Away Apeople Awill Afind Ait Ahard Ato Adecipher. AOther
Atimes Ayou Afeel Alike Ayou Ashould Aattach Asome Asimilar Athoughts Ato Ayour
Achunk Aof Athe Atext Acontained Ain Aa Aprogram, Abut Athe Acomputer Aignores
Ait. ATo Acode Aa Asingle Aline Acomment, Ause Athe Atwo Aslash Acharacters A(//),
AIt Ais Aused Ato Aattach Ablocks Aof Ainformation Aabout Aa Aprogram Aor Afile.
Summary
Now Ayou Aunderstand Athat Ayou Acan Adevelop Aa Aprogram Athrough Athe
Ause Aof Astatements, Awhich Astatement Aitself Acan Acontain Aseveral
ADisturbances Acan Aalso Acome Ainto Athe Aflow Aof Ainfluence Athrough
Aconditional A(if, Aelse, Aand Aswitch) Aand Alooping A(while, Ado, Aand Afor)
Astatements. AAnd Awe Atouched Abindings, Athey Acategorize Abits Aof Adata
Aunder Aa Aname, Aand Athey Acan Aalso Atrack Astate Awithin Athe Aprogram.
AThe Adefined Abindings Aenjoy Athe Aenvironment Abetter. AWe Aalso Atouched
Aon Athe Afunctions Asection Abeing Aunique Avalues Athat Asummarize Aa Apiece
Aargument2). AThis Atype Aof Afunction Acall Ais Aan Aexpression Aand Acan
Aprovide Avalue.
Exercise
Write Aa AJavaScript Aprogram Athat Adisplays Athe Alarger Aand Aaccepts
Adouble Aintegers.
Solution
It Ais Aimportant Ato Aknow Athat Ayou Acan Awrite A.length Aat Athe Aend Aof Aa
Astring Ato Afind Aits Alength.
Avalue Ahas Aa Alot Aof Aadvantages. AIt Aprovides Aa Away Ato Aorganize Amore
Defining Aa Afunction
A Afunction Ais Aa Asystematic Abinding Awhereby Aa Afunction Adefines Athe
Abinding Avalue. AThe Acode Abelow Aindicates Aa Afunction Athat Aprovides Athe
Aaccommodates Athe Ayet Ato Abe Aexecuted Astatements Awhen Ayou Acall Aa
Afunction. AThe Afunction Abody Ashould Aalways Abe Aenclosed Ain Abraces,
Aeven Awhen Ait Ais Ajust Aan Aindividual Astatement. AA Afunction Acan Acontain
AmakeNoise Acontains Ano Aparameter Anames Aand Apower Aconsists Aof Atwo:
Acontain Aan Aexpression Aat Athe Aend Awill Abe Areturned Aas Aundefined.
Every Abinding Aconsists Aof Aa Ascope Athat Aenables Athe Avisibility Aof Athe
Abinding. AWhenever Ayou Acall Aa Afunction, Anew Aillustrations Aof Abinding
Afunction Acall Abehaves Ain Aits Aown Aworld Aand Ais Aeasy Ato Aunderstand.
AFunctions Athat Awere Adeclared Awith Athe Avar Akeyword Ain Athe Apre-2015
AJavaScript Aand Acan Abe Aseen Athroughout Athe Aglobal Ascope. AThey Aare
let Ax A= A10;
if A(true) A{
let Ay A= A20;
var Az A= A30;
console.log(x A+ Ay A+ Az);
// A→ A60
}
// Ay Ais Anot Avisible Ahere
console.log(x A+ Az);
// A→ A40
Nested Ascope
JavaScript Adifferentiate Abetween Athe Aglobal Aand Alocal Abindings. AYou
Acan Acreate Ablock Aand Afunctions Awithin Athe Aother Afunctions Aand Ablocks
Awithin Aa Ablock Ais Adecided Aby Athe Ablock Aposition Ain Athe Aprogram Atext.
AEvery Alocal Ascope Acan Aaccess Athe Acontained Alocal Ascope Aand Aevery
Ascope Acan Aas Awell Aaccess Athe Aglobal Ascope. AThis Aconcept Ais Anamed
Alexical Ascoping.
Adefined. AFunctions Acan Abe Aused Ain Aarbitrary Aexpressions Aas Awell Aas
Aused Ato Asave Aa Afunction Avalue Ainto Aa Anew Abinding. AA Abinding Athat
Astores Aa Afunction Ais Aa Asystematic Abinding Aand Aa Anew Avalue Acan Abe
Aassigned Alike:
Afunction Adifferently.
function Asquare(x) A{
return Ax A* Ax;
}
This Astatement Adescribes Athe Abinding Asquare Aand Adirects Ait Aat Aa Astated
Afunction. AThis Atype Aof Afunction Adefinition Aconsists Aof Aone Aprecision.
Akeyword. AThe Aarrow A(=>) Acontains Aan Aequal Asign Aand Athe Agreater
Athan Acharacter.
Aexpression Ainstead Aof Aa Ablock Aof Abraces, Ataking Aback Athe Aexpression
Atask.
Aoperations.
function Agreet(who) A{
console.log("Hello A" A+ Awho);
}
greet("Harry");
console.log("Bye");
The Agreetings Acall Ainfluences Ato Acontrol Ato Ajump Ato Athe Abeginning Aof
Athe Afunction. AThe Afunction Acall Aconsole.log Agets Aits Ajob Adone Aand
Aseize Acontrol, Aand Athen Asend Acontrol Aback Ato Athe Afunction. AThe
Afunction Acycle Aends Athere Aand Atransfers Aback Ato Athe Aplace Athat Acalls
Aposition. AWhen AConsole.log Acompletes, Ait Amust Areturn Ato Athe Aend Aof
Athe Aprogram. AA Acall Astack Ais Aa Aplace Awhere Athe Acomputer Astores Athis
Acontext. AWhenever Ayou Acall Aa Afunction, Athe Apresent Acontext Asaves Aat
Athe Atop Aof Athis Astack. ABy Athe Atime Aa Afunction Agoes Aback, Ait
Aeradicates Athe Atop Acontext Aand Autilizes Athe Acontent Afor Acontinual
Aexecution.
The Astack Aneeds Aspace Ato Abe Asaved Ainto Awithin Athe Acomputer Amemory.
AThe Abelow Acode Aexplains Athis Aby Aquerying Athe Acomputer Athat Acreates
function Achicken() A{
return Aegg();
}
function Aegg() A{
return Achicken();
}
console.log(chicken() A+ A" Acame Afirst.");
// A→ A??
Optional AArguments
The Abelow Acode Afunctions Aproperly Awithout Ainterference:
function Asquare(x) A{return Ax A* Ax;}
console.log(square(4, Atrue, A"hedgehog"));
// A→ A16
The Asquare Ais Adescribed Awith Ajust Aone Aparameter Aand Awe Acall Ait Athree.
AThat Ais Apossible Abecause Athe Aprogramming Alanguage Adisregards Athe
Asurplus Aarguments Aand Arecognizes Athe Asquare Aof Athe Afirst Aone.
AJavaScript Ais Avery Atolerant Aabout Athe Aamount Aof Aarguments Abeing
Apassed Ato Aa Afunction. AThe Agood Aside Aof Athis Acharacter Ais Athat Ait
Awant Ato Apass Aand Anot Aproduce Athe Asecond Aargument, Athe Adefault
AThe Abelow Acode Adisplays Athis Aexample; Ait Adescribes Aa Awrap Avalue, Aa
function AwrapValue(n) A{
let Alocal A= An;
return A() A=> Alocal;
}
let Awrap1 A= AwrapValue(1);
let Awrap2 A= AwrapValue(2);
console.log(wrap1());
// A→ A1
console.log(wrap2());
// A→ A2
This Aconcept Ais Acalled Aclosure. AIt Agives Athe Auser Athe Aability Ato
Areference Aa Aparticular Ainstance Aof Aa Alocal Abinding Awithin Aa Aconfining
Ascope. AAdding Aa Afew Achanges, Aour Aprevious Aexample Acan Aturn Ainto Aa
Away Ato Adevelop Afunctions Athat Aaccumulates Aby Aan Aarbitrary Aamount.
function Amultiplier(factor) A{
return Anumber A=> Anumber A* Afactor;
}
let Atwice A= Amultiplier(2);
console.log(twice(5));
// A→ A10
Recursion
A Afunction Acan Acall Aitself Abut Ashould Anot Acall Aitself Aregularly Ato Aavoid
Astack Aoverflow. ARecursive Afunction Ais Aa Afunction Athat Acalls Aitself.
Athan Athe Amultiple Acalling Aof Afunctions. AAlthough Athat Adoes Anot Amake
Arecursion Aan Aineffective Aoption Aof Alooping, Afew Aproblems Aare Asolved
Awith Arecursion Aeasier Athan Awith Athe Ause Aof Aloops. AProblems Athat Aneed
Ainspecting Aor Aprocessing Aseveral Abranches. ACheck Athis Aout: Awe Abegin
Afrom Anumber A1 Aand Acontinuously Aadd A5 Aor Amultiply Aby A3. AFor
Ainstance, Athe Anumber A13 Acan Abe Aobtained Aby Amultiplying Aby A3 Aand
Athe Aaddition Aof A5 Atwice, Athereby Awe Acannot Aattain Athe A15. AThe Acode
Ais Athe Arecursive Asolution:
function AfindSolution(target) A{
function Afind(current, Ahistory) A{
if A(current A== Atarget) A{
return Ahistory;
} Aelse Aif A(current A> Atarget) A{
return Anull;
} Aelse A{
return Afind(current A+ A5, A`(${history} A+ A5)`) A||
find(current A* A3, A`(${history} A* A3)`);
}
}
return Afind(1, A"1");
}
console.log(findSolution(24));
// A→ A(((1 A* A3) A+ A5) A* A3)
The Ainner Afunction Afind Atakes Atwo Aarguments, Athe Acurrent Anumber Aand
Aa Astring Athat Adocuments Athe Aprocess Aof Aattaining Athis Anumber. AIf Aa
Asolution Ais Afound, Ait Asends Aback Aa Astring Athat Adisplays Athe Aroute Ato
Athe Atarget Aand Aif Ano Asolution Ais Afound, Athe Areturned Avalue Awill Abe
Anull. ATo Aachieve Athis, Athe Afunction Aexecutes Aone Aof Athree Aactions. AIf
Ayour Atarget Ais Athe Ais Athe Acurrent Anumber, Ayou Acan Aattain Athat Atarget
Aby Ausing Athe Acurrent Ahistory Aso Ait Ais Asent Aback. ASometimes Athe
Anumber Ais Alarger Athan Athe Atarget, Abut Ayou Ado Anot Aneed Ato Aexplore
Athis Aoption Abecause Aaddition Aand Asubtraction Acan Aonly Aenlarge Athe
Anumber Aso Ait Ais Asent Aback Aas Anull. AIf Ayou Aremain Abeneath Athe Atarget
Anumber, Aboth Apaths Athat Abegin Athe Acurrent Anumber Ais Atried Aby Acalling
Aitself Atwice, Aone Ato Aadd Aand Athe Aother Ato Amultiply. AIf Athe Afirst Acall
Asends Asomething Avalid Aback Ain Areturn, Athen Agood, Aif Anot, Areturn Athe
Asecond Acall, Ait Adoes Anot Amatter Awhether Aa Astring Aor Anull Ais Aprovided.
ABelow Ais Aan Aillustration Aof Ahow Afunctions Aprovide Aeffects. AThis
Aexample Asearches Afor Aa Aremedy Afor Athe Anumber A13.
find(1, A"1")
find(6, A"(1 A+ A5)")
find(11, A"((1 A+ A5) A+ A5)")
find(16, A"(((1 A+ A5) A+ A5) A+ A5)")
too Abig
find(33, A"(((1 A+ A5) A+ A5) A* A3)")
too Abig
find(18, A"((1 A+ A5) A* A3)")
too Abig
find(3, A"(1 A* A3)")
find(8, A"((1 A* A3) A+ A5)")
find(13, A"(((1 A* A3) A+ A5) A+ A5)")
found!
The Aindentation Aspecifies Aprofound Aof Athe Acall Astack.
Growing functions
A
Functions Acan Abe Aintroduced Ainto Aprograms Ain Atwo Aways. AThe Afirst Ais
Aby Awriting Asimilar Acodes Aa Alot Aof Atimes, Awhich Aenables Amore
Amistakes Awhile Athe Asecond Ais Ato Asearch Afor Aa Afew Afunctionalities Athat
Ahave Anot Abeen Awritten Aand Adeserves Aits Aown Afunction. AYou Acan Abegin
Aby Anaming Athe Afunction Aand Awrite Athe Abody. ABelow Ais Aan Aexample.
AWe Awill Awrite Aa Aprogram Athat Areproduce Atwo Anumbers–the Aamount Aof
007 ACows
011 AChickens
function AprintFarmInventory(cows, Achickens) A{
let AcowString A= AString(cows);
while A(cowString.length A< A3) A{
cowString A= A"0" A+ AcowString;
}
console.log(`${cowString} ACows`);
let AchickenString A= AString(chickens);
while A(chickenString.length A< A3) A{
chickenString A= A"0" A+ AchickenString;
}
console.log(`${chickenString} AChickens`);
}
printFarmInventory(7, A11);
Writing Alength Aafter Aa Astring Aexpression Adetermines Athe Alength Aof Athe
Aparticular Astring. AThe Aloop Acontinues Ato Aadd Azeros Aat Athe Abeginning
Aof Athe Anumber Astrings Auntil Athey Acomprise Aof Athree Acharacters.
Avalue Amanufacturing Afunction Athat Adoes Anot Adepend Aon Aeither Acode
Aside Aeffects. AFor Ainstance, Ait Awill Anot Arecognize Aa Aglobal Abinding Awith
Summary
In Athis Achapter, Ayou Anow Aunderstand Ahow Ato Awrite Ayour Afunctions.
AKnowing Ahow Ato Awrite Athe Afunction Akeyword Aand Awhen Ato Ause Aan
Aexpression, Ayou Acan Anow Abuild Aa Afunction Avalue. AWhen Ayou Ause Aa
Afunction Aas Aa Astatement, Ait Acan Aset Aa Abinding Aand Aname Aa Afunction Ato
Abe Aits Avalue. AYou Acan Aalso Ause Aarrow Afunctions Ato Abuild Afunctions.
Ain Aa Aspecific Ascope Aare Alocal Aand Ainvisible Afrom Athe Aoutside.
ABindings Aset Awith Avar Aact Adifferently, Aand Athey Aresult Ain Athe Aglobal
Avarious Afunctions Ais Aimportant. AYou Ado Anot Aneed Ato Arepeat Ayourself
Aoften, Aand Afunctions Acan Aarrange Aa Aprogram Aby Acategorizing Acode Ainto
Exercise
Write Aa AJavaScript Aprogram Ato Asee Aif Aa Anumber Ais Aeven Aor Anot.
Solution
A
Chapter 5: Data Structures: Objects and Arrays
Data Alayouts Adevelop Afrom ABooleans, Astrings, Aand Anumbers Afragments.
ASeveral Atypes Aof Ainformation Ause Amore Athan Aone Achunk. AObjects
Aenable Athe Auser Ato Acollate Avalues Atogether Awith Aother Aobjects Ato
Adevelop Amore Acompound Alayouts. AIn Athis Achapter, Ayou Awill Aunderstand
Data Asets
If Ayou Awant Ato Awork Awith Aa Alot Aof Adigital Adata, Ayou Amust Arepresent
Athe Adigital Adata Ain Athe Amachine Amemory. AFor Ainstance, Ayou Aneed Ato
Arepresent Aa Agroup Aof Anumbers Alike A2, A3, A5, A7, Aand A11. ANow Alet’s Aget
Aprolific Awith Aour Ause Aof Astrings Aas Astrings Acan Acontain Aquite Aa Alot Aof
Asequences Aof Avalues, Awhich Ais Acalled Aan Aarray. AWrite Aan Aarray Aas Aa
Alist Aof Avalues Awithin Athe Abetween Asquare Abrackets, Adifferentiated Aby
Acommas.
Acounting Aelements Ain Aan Aarray, Ayou Abegin Afrom Azero Aand Anot Aone.
Properties
The Aprevious Achapters Ahave Acontained Aexpressions Alike Athe AmyString.
ALength A(to Acalculate Athe Alength Aof Aa Astring) Aand AMath.max A(the
Aundefined. AIf Ayou Aaccess Aa Aproperty Awith Aa Anonvalue, Athe Aresult Awill
null.length;
// A→ ATypeError: Anull Ahas Ano Aproperties
In AJavaScript, Athe Atwo Amajor Aways Ato Aevaluate Aproperties Aare Awith Aa
Adot Aand Asquare Abrackets. AThe Avalue.x Aand Avalue[x] Acan Aevaluate Athe
Aproperty Aon Avalue, Anot Aparticularly Athe Asame Aproperty. AHow Ayou Ause
If Ayou Awant Ato Ause Athe Adot, Athe Atext Ayou Ainput Aafter Athe Adot Ais Athe
Aname Aof Athe Aproperty. AAnd Aif Ayou're Agoing Ato Ause Athe Asquare
Abrackets, Athe Aexpression Awithin Athe Abrackets Ais Aaccessed Ato Aredeem
Athe Aproperty Aname. AValue.x Adelivers Athe Aproperty Aof Avalue Acalled A“x,”
Atransformed Ainto Aa Astring Aas Athe Aname Aof Athe Aproperty. AIf Athe
Aproperty Ayou Awant Ais Anamed Acolor, Ayou Ashould Atype Avalue.color. AYou
Acan Awithdraw Athe Aproperty Athat Athe Avalue Acalled Awithin Athe Abinding Ai,
Atype Avalue[i]. AThe Aname Aof Aproperties Aare Astrings, Abut Athe Adot
Anotation Aperforms Awith Aonly Avalid Abinding Anames. ATherefore Aif Ayou
Awant Ato Aevaluate Aa Aproperty Anamed A2 Aor AJohn ADoe, Ause Athe Asquare
The Aelements Awithin Aan Aarray Asave Aas Athe Aproperties Aof Aan Aarray Athat
Autilize Anumbers Aas Aproperty Anames. AAn Aarray's Alength Aproperty
Adetermines Ahow Amany Aitems Ait Acontains. AThe Aname Aof Athat Aproperty
Ais Aa Avalid, Abinding Aname. AType Aarray.length Ato Adetermine Athe Alength
Aof Aan Aarray, Ait Ais Amuch Aeasier Ato Atype Athan Athe Aarray["length"].
Methods
The Astring Aand Aarray Aobjects Aaccommodate Aseveral Aproperties Athat
Astore Afunction Avalues.
Ado Anot Ago Athrough Aan Aargument, Athe Afunction Acan Aevaluate Athe Astring
A"Doh," Awhich Ais Athe Aproperty Aof Athe Avalue Awe Acalled Aabove.
AProperties Athat Aconsist Aof Afunctions Aare Aknown Aas Amethods. AThe
Abelow Aexample Aillustrates Atwo Amethods Athat Acan Abe Aused Ato Ainfluence
Aarrays.
Avalue Awithin Aan Aarray Aand Asends Ait Aback. AThese Aare Agenerational
Aterms Aof Aoperations Aon Aa Astack. AA Astack Ais Aa Adata Alayout Athat
Aenables Ausers Ato Apush Avalues Ain Aand Apop Athem Aout Ain Athe Aopposite
Objects
You Acan Arepresent Aa Agroup Aof Alog Aentries Aas Aan Aarray Aalthough Aeach
Astring Aentry Ais Arequired Ato Asave Aa Alist Aof Aactivities Aas Awell Aas Aa
ABoolean Avalue Athat Aspecifies Aif AJacques Atransformed Ato Aa Asquirrel Aor
Anot. AWe Awill Agroup Athis Ainto Aa Asingle Avalue Aand Aplace Athe Agrouped
Avalues Awithin Aan Aarray Aof Alog Aentries. AArbitrary Acollections Aof
Aproperties Aare Athe Avalue Aof Athe Atype Aobject. AWe Awill Adevelop Aan
Aobject Autilizing Abraces Aas Aan Aexpression.
let Aday1 A= A{
squirrel: Afalse,
events: A["work", A"touched Atree", A"pizza", A"running"]
};
console.log(day1.squirrel);
// A→ Afalse
console.log(day1.wolf);
// A→ Aundefined
day1.wolf A= Afalse;
console.log(day1.wolf);
// A→ Afalse
The Abraces Acontain Aa Alist Aof Aproperties Adivided Aby Acommas. AEvery
Aproperty Acontains Aa Aname Aaccompanied Aby Aa Avalue Aand Aa Acolon.
AWhen Ayou Awrite Aan Aobject Aover Aseveral Alines, Amake Asure Ayour
Aindenting Ais Agood Abecause Ait Aenables Abetter Areadability. AQuote AInvalid
let Adescriptions A= A{
work: A"Went Ato Awork,"
"touched Atree": A"Touched Aa Atree."
};
You Acan Ause Abraces Ain Atwo Adifferent Aways Ain AJavaScript. AIt Ais Aused Aat
Athe Abeginning Aof Aa Astatement Aand Acan Aalso Abe Autilized Ato Abegin Aa
Aexpression Ausing Athe A= Aoperator. AIt Atakes Athe Aplace Aof Athe Aproperty
Avalue, Aand Aif Ait Ais Ain Aexistence, Ait Adevelops Aa Anew Aone. AThe Adelete
Aoperator Ais Aa Aunary Aoperator Athat Aeradicates Athe Aproperty Aname Aof Aan
Aproperty Ato Aundefined Aand Adeleting Ait Ais Athat Athe Aobject Aconsists Aof Aa
Aproperty Aand Athe Adeleting Adoes Anot Apossess Athe Aproperty, Aso Athe Avalue
Areturns Aas Afalse. ATo Adetermine Awhat Aproperties Acontained Ain Aan Aobject
Aand Asends Aback Aan Aarray Aof Astrings Awith Aproperty Anames Aof Athe
Aobject.
Aillustration Astands Afor Athe Ajournal Athat AJacques Astores Aas Aan Aarray Aof
Aobjects.
let Ajournal A= A[
{events: A["work", A"touched Atree", A"pizza",
"running", A"television"],
squirrel: Afalse},
{events: A["work", A"ice Acream", A"cauliflower",
"lasagna", A"touched Atree", A"brushed Ateeth"],
squirrel: Afalse},
{events: A["weekend", A"cycling", A"break", A"peanuts",
"beer"],
squirrel: Atrue},
/* Aand Aso Aon... A*/
];
Mutability
We Ahave Adiscussed Adifferent Avalues Asuch Aas ABooleans, Astrings, Aand
Anumbers Awhose Avalues Aare Adifficult Ato Achange, Aalthough Athey Acan Abe
Acombined Ato Aacquire Anew Avalues Afrom Athem. AObjects Aare Adifferent
Afrom Avalues, Aand Ait Aenables Aproperty Achanging; Athey Acan Acreate
Adifferent Acontent Afor Aa Asingle Aobject Avalue Aat Adifferent Atimes. AWhen
Athere Aare Atwo Anumbers, A120 Aand A120, Athey Aare Aconsidered Athe Asame,
Aand Athere Ais Aa Asimilarity Abetween Asetting Atwo Areferences Ato Aone
Aidentical Aby Anature. AThe Abinding Aobject3 Apoints Ato Aa Aseparate Aobject
Athat Aconsists Aof Asimilar Aproperties Aas Athe Aobject1 Abut Ahas Aa Adifferent
Alife. ABindings Acan Abe Aconstant Aor Achangeable, Abut Ait Adoes Anot
Ainfluence Atheir Avalues. AA Aconst Abinding Aan Aobject Acannot Abe
Atransformed Aand Arelentlessly Apointing Ato Athe Asame Aobject. AIt Ais Athe
So Athen, Aat A10 Ap.m. Aevery Aevening Aor Aoccasionally Ain Athe Afollowing
Amorning, Aafter
getting Adown Afrom Athe Atop Ashelf Aof Ayour Abookcase, Ayour Adaily
Arecords.
Asignifies Athat Athe Atwo Aare Arelated. ANegative Ameans Athat Athe Avariables
Aare Arelated Aperfectly Abut Aopposites Ato Aeach Aother; Aone Ais Atrue, Aand
Athe Aother Ais Afalse. ATo Acalculate Athe Aevaluation Aof Athe Acorrelation
Abetween Atwo ABoolean Avalues, Autilize Athe Aphi Acoefficient A(φ). AThis
Aformula Ainput Ais Aa Afrequency Atable Aholding Athe Anumber Aof Atimes Ait
Anotices Aseveral Amixtures Aof Athe Avariables. AThe Aformula's Aoutput Awill
Abe Aa Anumber Abetween A-1 Aand A1. AThat Ais Athe Abest Adefinition Aof Aa
Acorrelation.
Let Aus Ause Athe Aevent Aif Aeating Apizza Aas Aan Aexample Aand Ainsert Ait Ainto
Aa Afrequency Atable Awhereby Aevery Anumber Asignifies Athe Atotal Aof Atimes,
Aand Awe Aused Athe Acombination Ain Aour Ameasurements. AWe Aname Athe
φ A=
n11n00 A− An10n01
√
n1•n0•n•1n•0
The Anotation An01 Asignifies Athe Anumber Aof Ameasurements Awhere Athe
Afirst Avariable Aturns Afalse A(0), Aand Athe Asecond Avariable Abecomes Atrue
The Avalue An1• Aindicates Athe Asum Aof Aall Ameasurements Awhere Athe Afirst
Avariable Aturns Atrue, Athat Ais A5 Ain Athe Atable Aexample. AAs Awell Aas An•0
Asuggests Athe Aquantity Aof Athe Ameasurements Awhere Athe Asecond Avariable
Abecomes Afalse.
The Apizza Atable, Athe Atop Aof Athe Adivision Aline Awould Abe A1×76−4×9 A=
A40, Aand Athe Abelow Apart Abelow Awould Abe Athe Asquare Aroot Aof
A5×85×10×80, Aor A√340000. AThe Aresult Awill Abe Aφ A≈ A0.069, Awhich Ais
Computing Acorrelation
JavaScript Aenables Athe Arepresentation Aof Aa Atwo-by-two Atable Awith Afour
Aelement Aarrays A([76, A9, A4, A1]). AOther Arepresentations Asuch Aas Aan Aarray
Aholding Atwo Atwo-element A([[76, A9], A[4, A1]]) Aor Aan Aobject Acontaining
Aproperty Anames Asuch Aas A"11" Aand A"01", Aand Athe Aflat Aarray, Awhich
Aproduces Athe Aexpression Athat Aaccess Athe Achair Ashort. AWe Awill Aexplain
Athe Aindices Ato Athe Aarray Aas Atwo-bit Abinary Anumbers, Awhere Athe Amost
Asignificant Adigit Arefers Ato Athe Asquirrel Avariable Awhile Athe Aleast
Asignificant Arefers Ato Athe Aevent Atable. AFor Ainstance, Athe Abinary Anumber
A10 Arefers Ato Athe A"Jacques Adid Aturn Ainto Aa Asquirrel" Acase Abut Athe
Aevents Adidn’t Aappear. AIt Aoccurred Afour Aties Aand Abinary Ais A2 Ain
Adecimal, Athe Anumber Ais Astored Aas Aan Aindex A2 Aof Athe Aarray. AThis
function Aphi(chair) A{
return A(chair A[3] A* Achair A0] A- Achair A[2] A* Achair A[1]) A/
Math.sqrt((chair A[2] A+ Achair A[3]) A*
(chair A[0] A+ Achair A[1]) A*
(chair A[1] A+ Achair A[3]) A*
(chair A[0] A+ Achair A[2]));
}
console.log(phi([76, A9, A4, A1]));
// A→ A0.068599434
The Atable Arequires Atwo Afields Ato Aacquire Afields Alike An1• Abecause Athe
Atotal Anumber Aof Arows Aand Acolumns Aare Astored Aindirectly Ainto Aour Adata
Alayout. AIf Ayou Awant Ato Aextract Aa Atwo-by-two Atable Afor Aa Aparticular
Aevent, Ayou Amust Aloop Aacross Aall Athe Aentries Aand Acalculate Athe Anumber
Adecide Aif Athe Aevent Aname Ais Ainterested Athe Astated Aevent Alist Afor Aa
Agiven Aday.
Array loops
A
Alayouts.
Aevent.
A Alot Aof Acorrelations Aappear Ato Alie Aclose Ato Azero. AEating Abread,
Acarrots Aor Apudding Adoes Anot Aactivate Athe Asquirrel-lycanthropy. AIt
Ahappens Aoften Aduring Athe Aweekends. AUsing Athat, Abelow Aare Aall Athe
Acorrelations.
Further Aarrayology
Here Aare Aa Afew Amore Aobject-related Aconcepts Ayou Ashould Aknow. AWe
Abegin Awith Athe Ause Aof Asome Auseful Amethods Aof Aan Aarray.
The Aadding Aand Aremoving Aof Amethods Athings Aat Athe Abeginning Aof Aan
Aarray Aare Acalled Aunshift Aand Ashift.
Ayou Awant Ato Aget Asomething Adone, Acall AgetTask() Ato Aget A(and Aremove)
Aitem Afrom Athe Aqueue. AIf Ayou Awant Ato Asearch Afor Aa Aparticular Avalue,
Aarrays Asupply Aan AindexOf Amethod. AThe Amethod Afinds Aits Away Athrough
Athe Aarray Afrom Athe Abeginning Ato Athe Aend Aand Asends Aback Athe Aindex
Athe Avalue Arequested, Aif Ait Awas Afound Ait Areturns A—or A-1 Aif Ait Awasn’t
Afound. ATo Asearch Afrom Athe Abeginning Ato Athe Aend, Ause Athe AlastIndexOf
Amethod.
Athis Amethod Abegins Aand Aends Athe Aindices Aand Asend Aback Aan Aarray
Acontaining Athree Aelements. AThe Abeginning Ais Ainclusive Awhile Athe Aend
Ais Aexclusive.
Ato Aproduce Aa Anew Aarray. AThe Abelow Aexample Adisplays Aconcat Aand
Aelement.
console.log(String(6).padStart(3, A"0"));
// A→ A006
You Acan Abreak Aa Astring Aon Aevery Adevelopment Aof Aanother Astring Awith
Asplit Aand Athen Ajoin Athem Atogether Awith Ajoin.
Atogether.
console.log("LA".repeat(3));
// A→ ALALALA
If Ayou Awant Ato Aaccess Aindividual Acharacters Ain Aa Astring. Alook Abelow:
let Astring A= A"ABC";
console.log(string.length);
// A→ A3
console.log(string[1]);
// A→ Ab
Rest Aparameters
It Ais Aimportant Afor Aa Afunction Ato Areceive Aany Anumber Aof Aarguments. AIf
Ayou Awant Ato Awrite Asuch Aa Afunction, Aadd Athree Adots Abefore Athe Alast
Athree Adot Anotation Ato Acall Afunctions Awithin Aan Aarray Aof Aarguments.
Anew Aarray.
Aand Afunctions Awithout Aglobal Abindings. AThis Ais Athe Aold Away Ato Awrite
function ArandomPointOnCircle(radius) A{
let Aangle A= AMath.random() A* A2 A* AMath.PI;
return A{x: Aradius A* AMath.cos(angle),
y: Aradius A* AMath.sin(angle)};
}
console.log(randomPointOnCircle(2));
// A→ A{x: A0.3667, Ay: A1.966}
The Amath Afunction Asends Aback Aa Anew Apseudo Arandom Anumber Abetween
Azero Aand Aone Awhenever Ayou Acall Ait.
console.log(Math.random());
// A→ A0.36993729369714856
75
console.log(Math.random());
// A→ A0.727367032552138
console.log(Math.random());
// A→ A0.40180766698904335
When Ayou Aneed Aa Awhole Arandom Anumber Aand Anot Athe Afractional Aone,
Ayou Acan Autilize Athe AMath.floor Aon Athe Aoutcome Aof Athe AMath.random.
console.log(Math.floor(Math.random() A* A10));
// A→ A2
The Arandom Anumber Ais Amultiplied Aby A10 Aand Aoutputs Aa Anumber
Agreater Athan Aor Aequal Ato A0 Aand Abelow A10. ARemember Athe AMath.floor
Arounds Adown, Athe Aoutput Awill Abe Aany Anumber Afrom A0 Athrough A9. AThe
Arounds Aup Ato Aa Awhole Anumber, Aand Athe AMath.round, Awhich Arounds Aup
Ato Athe Anearest Awhole Anumber, Aand AMath.abs, Athis Afunction Atakes Athe
Destructuring
Let’s Ause Athe Aphi Afunction Aa Alittle Abit.
function Aphi(chair) A{
return A(table[3] A* Achair[0] A- Atable[2] A* Achair[1]) A/
Math.sqrt((chair A[2] A+ Achair A[3]) A*
(table[0] A+ Achair A[1]) A*
(table[1] A+ Achair A[3]) A*
(table[0] A+ Achair A[2]));
}
One Aof Athe Afew Areasons Athis Afunction Ais Ahard Ato Aread Ais Abecause
Athere Ais Aa Abinding Adirected Ato Athe Aarray, Abut Awe Awant Abindings Afor
Athe Aelements Aof Athe Aarray, Athat Ais, Alet An00 A= Atable[0] Aand Aso Aon.
AAlthough, Athere Ais Aa Abetter Away Ato Aget Athis Adone Ain AJavaScript.
Autilize Athe Asquare Abrackets Ato Acheck Afor Athe Avalue Aof Aits Abinding
Acontents. AUse Athe Asame Aconcept Afor Aobjects, Autilize Athe Abraces Aand
Asimilar Ato AJavaScript Ain Aits Awriting Astyle Aof Aarrays Aand Aobject,
Aalthough Athere Aare Arestrictions. AIts Aproperty Anames Ashould Abe Aenclosed
Awithin Adouble Aquotes, Ait Aallows Aonly Asimple Adata Aexpressions, Abinding
Here Ais Aa Asample Aof Aa Ajournal Aentry Arepresented Aas AJSON Adata:
{
"squirrel": Afalse,
"events": A["work", A"touched Atree", A"pizza", A"running"]
}
JavaScript Aconsists Aof Athe Afunctions AJSON.stringify Aand AJSON.parse Ato
Atransform Adata Ato Aand Afrom Athis Aformat. AJSON.stringify Atakes Aa
AJSON.parse Atakes Aa Astring Aand Atransforms Ait Ato Aits Aencoded Avalue.
Summary
We Acovered Athe Aobjects Aand Aarrays Ain Athis Achapter, Aand Athey Acreate
Aways Ato Acategorize Avarious Avalues Ain Aa Asingle Avalue. AFancifully, Athis
Aenables Ayou Ato Ainsert Aseveral Aassociated Aitems Ain Aa Abag Aand Amove
Aabout Awith Athe Abag, Arather Athan Aenclosing Ayour Aarms Aaround Aevery
Asingular Athing Aand Atrying Ato Akeep Athem Adifferently. AA Alot Aof Avalues
Adefined Aset Aof Athem. AArrays, Aas Awell Acontain Adifferent Aamounts Aof
Asimilar Afanciful Avalues Aand Autilize Athe Anumbers A(beginning Afrom A0) Aas
Athe Aproperty’s Anames. AArrays Acontain Afew Asets Aof Aproperties, Awhich
Aare Athe Alength Aand Aseveral Amethods. AMethods Aare Afunctions Athat
Areside Awithin Aproperties Aand Abehave Aon Athe Avalue Athey Aare Aits
Aproperty. AYou Acan Aloop Aacross Aarrays Athrough Aa Aunique Atype Aof Afor
Exercise
Write Aa AJavaScript Afunction Ato Aobtain Athe Afirst Aitem Ain Aan Aarray.
ASetting Aa Aparameter A'n' Awill Asend Aback Athe Afirst A'n' Aelements Aof Athe
Aarray.
Data:
console.log(array_Clone([1, A2, A4, A0]));
console.log(array_Clone([1, A2, A[4, A0]]));
[1, A2, A4, A0]
[1, A2, A[4, A0]]
Solutions
Chapter 6: Higher-Order Functions
A Alarge Aprogram Atakes Atime Ato Adevelop Aand Aproduces Aa Alot Aof Aspace
Afor Abugs Ato Ahide, Awhich Ain Aturn Amakes Athem Ahard Ato Afind. AHere Aare
The Afirst Ais Aself-contained, Aand Athe Astatement Ais Asix Alines Along.
let Atotal A= A0, Acount A= A1;
while A(count A<= A10) A{
total A+= Acount;
count A+= A1;
}
console.log(total);
The Asecond Adepends Aon Atwo Aexternal Afunctions, Aand Athe Astatement Ais
Aone Aline Along.
console.log(sum(range(1, A10)));
Abstraction
When Ait Acomes Ato AJavaScript Aprogramming Alanguage, Avocabulary’s Ause
Athe Aterm Aabstraction. AAbstraction Ais Aused Ato Ahide Adetails Aand Agives
Athe Auser Athe Acapability Ato Atalk Aabout Aproblems Aon Aa Ahigher Alevel. ALet
Per Aperson, Aplace A1 Acup Aof Adried Apeas Awithin Aa Acontainer. APut Aenough
Awater Ato Acover Athe Apeas, Alet Ait Asoak Afor Aabout A12 Ahours. ARemove Ait
Afrom Athe Awater Aand Aplace Ait Ain Aa Apot Aused Afor Acooking Awith Athe
Aaddition Aof Awater Afour Acups Aof Awater A(4 Acups). ALet Ait Aboil Afor Aabout
Atwo Ahours, Aper-person Ahold Ahalf Aof Aan Aonion, Aslice Ait Ausing Aa Aknife,
Aand Apour Ait Ainto Athe Apeas, Alet Ait Aprepare Afor A10 Aminutes.
Dip Athe Apeas Ainto Athe Awater Afor Aabout A12 Ahours. ABoil Ait Afor Aabout A2
Ahours Ain Awater A(4 Acups), Aattach Aand Aslice Avegetables. ACook Ait Afor A10
Aminutes.
The Acomputer Aperforms Atasks Aone Aby Aone, Afrom Ablind Ato Ahigh-level
Aconcepts.
Abstracting Arepetition
Plain Afunctions Aare Aused Ato Acreate Aabstractions. AIt Ais Ahabitual Afor Aa
Aprogram Ato Arepeat Aa Aparticular Afunction Aa Aspecified Aamount Aof Atime.
83
for A(let Ai A= A0; Ai A< A10; Ai++) A{
console.log(i);
}
Here Awe Awrite Aa Afunction Athat Acalls Aconsole.log AN Atimes.
function ArepeatLog(n) A{
for A(let AI A= A0; AI A< An; AI++) A{
console.log(I);
}
}
If Ayou Awant Ato Aperform Aa Atask Adifferent Afrom Alogging Ain Anumbers,
Ayou Acan Aroll Aaction Aas Aa Afunction Avalue.
Higher-order Afunctions
Higher-order Afunctions Aare Afunctions Athat Awork Aon Aother Afunctions
Awhether Aby Aarguments Aor Aby Asending Athem Aback. AThis Atype Aof
Afunction Aenables Auser Ato Aabstract Aover Aactions. AFor Ainstance, Ayou Ahave
function AgreaterThan(n) A{
return Am A=> Am A> An;
}
let AgreaterThan10 A= AgreaterThan(10);
console.log(greaterThan10(11));
// A→ Atrue
You Acan Aalso Ahave Afunctions Athat Acan Atransform Aother Afunctions.
function Anoisy(f) A{
return A(...args) A=> A{
console.log("calling Awith", Aargs);
let Aresult A= Af(...args);
console.log("called Awith", Aargs, A", Areturned", Aresult);
return Aresult;
};
}
noisy(Math.min)(3, A2, A1);
// A→ Acalling Awith A[3, A2, A1]
// A→ Acalled Awith A[3, A2, A1], Areturned A1
You Acan Aas Awell Awrite Afunctions Athat Aproduce Anew Aset Aof Acontrol
Aflow.
Aarray.
Areceive.
Avalues. AThe Anew Aarray Aconsists Aof Athe Asame Alength Awith Athe Ainput
Aarray Abut Awill Amap Aout Aits Acontent Ato Aa Arenewed Afor Aby Athe Afunction.
Acombination Aand Abegin Aa Avalue. AThis Afunction Ais Amuch Aeasier Ato
function AcharacterCount(script) A{
return Ascript.ranges.reduce((count, A[from, Ato]) A=> A{
return Acount A+ A(to A- Afrom);
}, A0);
}
console.log(SCRIPTS.reduce((a, Ab) A=> A{
return AcharacterCount(a) A< AcharacterCount(b)? Ab: Aa;
}));
// A→ A{name: A"Han", A…}
Composability
Take Asome Atime Ato Athink Aabout Ahow Along Aour Alist Aof Acode Awould
Ahave Abeen Awithout Athe Ahigher-order Afunctions.
Aaverage Ayear Afor Aliving Aand Adead Ain Ahumans’ Ascripts Ain Athe Adata Aset.
function Aaverage(array) A{
return Aarray.reduce((a, Ab) A=> Aa A+ Ab) A/ Aarray.length;
}
console.log(Math.round(average(
SCRIPTS.filter(s A=> As.living).map(s A=> As.year))));
// A→ A1165
console.log(Math.round(average(
SCRIPTS.filter(s A=> A!s.living).map(s A=> As.year))));
// A→ A204
The Ablock Aof Acode Adisplays Athat Athe Adeath Ascript Ais Aolder Athan Athe
Aliving Aones.
Let’s Abegin Awith Athe Aentire Ascript, Afilter Aout Athe Adead A(or Aliving),
Aeradicate Atheir Ayears, Aaverage Athem Aand Acomplete Athe Aresult. AYou Acan
Anumbers Aonly.
One Aof Athe Asignificant Ause Aof Adata Aset Ais Ato Afind Aout Awhat Ascript Aa
Ablock Aof Atext Ais Ausing. ABelow Ais Aa Aprogram Athat Aperforms Athat Atask.
Every Ascript Acontains Aan Aarray Aof Acharacter Acode Aranges Arelated Awith
Ait. ASo, Aif Ayou Aare Agiven Aa Acharacter Acode, Autilize Athis Afunction Ato
function AcharacterScript(code) A{
for A(let Ascript Aof ASCRIPTS) A{
if A(script.ranges.some(([from, Ato]) A=> A{
return Acode A>= Afrom A&& Acode A< Ato;
})) A{
return Ascript;
}
}
return Anull;
}
console.log(characterScript(121));
// A→ A{name: A"Latin", A…}
Another Ahigh-order Afunction Ais Athe Asame Amethod. AThis Amethod Agets Aa
Atest Afunction Aand Adetermine Aif Athat Afunction Asends Aback Atrue Afor Aany
There Aare Asome Aoperations Aon AJavaScript Astrings Alike Asecuring Atheir
Alength Ausing Athe Alength Aproperty Aand Aevaluating Atheir Acontents
Aargument Asent Ato AcodePointAt Aremains Aan Aindex Ainto Athe Asuccession
Aof Acode Aunits. AIf Ayou Ause Athe AcodePointAt Ato Aloop Aacross Aa Astring, Ait
Asends Aback Aa Alist Aof Aobjects. AEach Aof Athe Aobjects Anames Aa Agroup Aand
The Amethod AfindIndex Ais Asimilar Ato AindexOf. AThis Amethod Ais Aused Ato
Asearch Afor Athe Afirst Avalue Afor Awhich Athe Astated Afunction Asends Aback
Atrue Aand Areturns A-1 Awhen Azero Aelements Ais Afound. AYou Acan Ause Athe
AcountBy Ato Adetermine Awhich Ascript Ais Autilized Ain Aa Ablock Aof Atext.
function AtextScripts(text) A{
let Ascripts A= AcountBy(text, Achar A=> A{
let Ascript A= AcharacterScript(char.codePointAt(0));
return Ascript? Ascript.name A: A"none";
}).filter(({name}) A=> Aname A!= A"none");
let Atotal A= Ascripts.reduce((n, A{count}) A=> An A+ Acount, A0);
if A(total A== A0) Areturn A"No Ascripts Afound";
return Ascripts.map(({name, Acount}) A=> A{
return A`${Math.round(count A* A100 A/ Atotal)}% A${name}`;
}).join(", A");
}
console.log(textScripts('英国的狗说"woof", A俄罗斯的狗说"тяв"'));
// A→ A61% AHan, A22% ALatin, A17% ACyrillic
The Afunction Acalculates Athe Acharacters Aby Aname Athrough Athe Ause Aof
AcharacterScript Ato Aattach Aa Aname Aand Areturning Aback Ato Athe Astring
A"none" Afor Acharacters Awithout Aany Ascript. AIf Ayou Awant Ato Acalculate
Apercentages, Ayou Aneed Ato Adetermine Athe Atotal Aamount Aof Acharacters
Abelonging Ato Aa Ascript Athat Athe Areduce Amethod Acan Areduce. AIf Ait Afinds
Summary
Having Athe Aability Ato Asend Afunction Avalues Ato Aseveral Afunctions Ais Aan
Aessential Aaspect Aof AJavaScript. AIt Aenables Awriting Afunctions Athat
Adeclares Athese Afunctions Acan Acontain Athe Agaps Aby Agiving Afunction
AforEach Ato Aloop Aacross Aelements Awithin Aan Aarray. AUse Athe Afilter
Amethod Ato Asend Aback Aa Anew Aarray Athat Ahas Aelements Athat Aconvey Athe
Apredicate Afunction. AChanging Aan Aarray Aby Asetting Aeach Aitem Athrough Aa
Afunction Ausing Athe Amap. AUse Areduce Ato Amerge Aevery Acomponent Aof
Exercises
Illustrate Aa AJavaScript Afunction Athat Atakes Aan Aarray Aof Anumbers Asaved
Aand Asearch Afor Athe Asecond-lowest Aand Asecond Ahighest Anumbers.
Solution
Chapter 7: The Secret Life
of Objects
When Ait Acomes Ato APrograming Alanguages, Athere Ais Aa Aset Atechnique
Athat Autilizes Aobjects Aas Aa Afundamental Aidea Aof Aprogram Aorganization,
Aand Athey Aare Acalled Athe Aobject-oriented Aprogramming. AIts Aconcept Awill
Encapsulation
The Aprimary Aimportance Aof Aobject-oriented Aprogramming Ais Ato Abreak
Aprograms Ainto Alittle Apieces Aand Amakes Asure Aeach Apiece Amanages Aits
Aeach Aother Ausing Ainterfaces, Aa Arestricted Aset Aof Afunctions Aor Abinding
Aobjects, Aand Atheir Alayout Acontains Aa Aparticular Aset Aof Aproperties Aand
Amethods. AProperties Awithin Athe Alayout Aare Apublic Awhile Athe Aouter Acode
Adocumentation. AIt Aregularly Auses Athe Aunderscore A(_) Acharacter Aat Athe
Methods
Methods Aare Aproperties Athat Agrasp Afunction Avalues.
let Arabbit A= A{};
rabbit.speak A= Afunction(line) A{
console.log(`The Arabbit Asays A'${line}'`);
};
rabbit.speak("I'm Aalive.");
// A→ AThe Arabbit Asays A'I'm Aalive.'
When Ayou Acall Aa Afunction Aas Aa Amethod, Athe Abinding Acalls Ait Ain Aits
Abody Aand Adirect Athe Aobject Ait Awas Acalled Aon.
function Aspeak(line) A{
console.log(`The A${this.type} Arabbit Asays A'${line}'`);
}
let AwhiteRabbit A= A{type: A"white", Aspeak};
let AhungryRabbit A= A{type: A"hungry", Aspeak};
whiteRabbit.speak("Oh Amy Awhiskers," A+ A"how Alate Ait's Agetting!");
// A→ AThe Awhite Arabbit Asays A'Oh Amy Awhiskers, Ahow
// Alate Ait's Agetting!'
HungryRabbit.speak("I Acould Ause Aa Acarrot Aright Anow.");
// A→ AThe Ahungry Arabbit Asays A'I Acould Ause Aa Acarrot Aright Anow.'
Arrow Afunctions Aare Ado Anot Abind Atheir Aown. AYou Acan Awrite Athe
Afollowing Acode:
function Anormal() A{
console.log(this.coords.map(n A=> An A/ Athis.length));
}
normal.call({coords: A[0, A2, A3], Alength: A5});
// A→ A[0, A0.4, A0.6]
Prototypes
Keep Aan Aeye Aout Aon Athis.
let Aempty A= A{};
console.log(empty.toString);
// A→ Afunction AtoString()…{}
console.log(empty.toString());
// A→ A[object AObject]
A Aproperty Ais Apulled Aout Aof Aan Aempty Aobject. AA Alot Aof AJavaScript
Aobjects Aconsists Aof Aa Aprototype. AThis Ais Aan Aobject Aused Aas Aan
console.log(Object.getPrototypeOf({}) A==
Object.prototype);
// A→ Atrue
console.log(Object.getPrototypeOf(Object.prototype));
// A→ Anull
Object.getPrototypeOf Asends Aback Athe Aprototype Aof Aan Aobject. AThis
Aprototype Aproduces Asome Amethod Athat Adisplays Ain Aevery Aobject Alike
AtoString Athat Atransforms Aan Aobject Ato Aa Astring Aexecution.
A Alot Aof Aobjects Ado Anot Acontain AObject.prototype Aas Atheir Aprototype
Abut Ause Aanother Aobject Athat Aproduces Aa Aseparate Aset Aof Adefault
Aproperties;
.prototype.
console.log(Object.getPrototypeOf(Math.max) A==
Function.prototype);
// A→ Atrue
console.log(Object.getPrototypeOf([]) A==
Array.prototype);
// A→ Atrue
This Atype Aof Aprototype Aobject Acontains Aa Aprototype, Athe
AObject.prototype, Aso Athat Ait Acan Acreate Amethods Asuch Aas AtoString
Aindirectly. AUse Athe AObject.create Ato Abuild Aan Aobject Awith Aa Aparticular
Aprototype.
let AprotoRabbit A= A{
speak(line) A{
console.log(`The A${this.type} Arabbit Asays A'${line}'`);
}
};
let AkillerRabbit A= AObject.create(protoRabbit);
killerRabbit.type A= A"killer";
killerRabbit.speak("SKREEEE!");
// A→ AThe Akiller Arabbit Asays A'SKREEEE!
They Aspeak(line) Aproperty Ain Aan Aobject Aexpression Aproduces Aa Aproperty
Acalled Aspeak Aand Aset Aa Afunction Aas Aits Avalue. AIt Ais Aa Ashorthand Away
Aas Amethods. AIf Ayou Awant Ato Abuild Aan Ainstance Aof Aa Aset Aclass, Acreate
Aan Aobject Athat Areceives Afrom Athe Anormal APrototype. AMake Asure Ait
Acontains Aproperties Athat Ashould Abe Aincluded Ain Athe Ainstance Aof Athis
function AmakeRabbit(type) A{
let Arabbit A= AObject.create(protoRabbit);
rabbit.type A= Atype;
return Arabbit;
}
JavaScript Aoffers Aa Away Ato Amake Adescribing Athis Afunction Aeffortlessly.
ASet Athe Akeyword Aat Athe Afront Aof Aa Afunction Acall. AThe Aprototype
Aobject Autilized Aduring Athe Aconstruction Aof Aobjects Ais Aattained Aby
function ARabbit(type) A{
this.type A= Atype;
}
Rabbit.prototype.speak A= Afunction(line) A{
console.log(`The A${this.type} Arabbit Asays A'${line}'`);
};
let AweirdRabbit A= Anew ARabbit("weird");
Function.prototype Ais Athe Aprototype Aof Aa Aconstructor. AIts Aprototype
Aproperty Agrasps Athe Aprototype Autilized Afor Ainstances Abuilt Athrough Ait.
console.log(Object.getPrototypeOf(Rabbit) A==
Function.prototype);
// A→ Atrue
console.log(Object.getPrototypeOf(weirdRabbit) A==
Rabbit.prototype);
// A→ Atrue
Class Anotation
JavaScript Aclasses Aare Aconstructor Afunctions Awith Aa Aprototype Aproperty.
ABelow Ais Aa Asample:
class ARabbit A{
constructor(type) A{
this.type A= Atype;
}
speak(line) A{
console.log(`The A${this.type} Arabbit Asays A'${line}'`);
}
}
let AkillerRabbit A= Anew ARabbit("killer");
let AblackRabbit A= Anew ARabbit("black");
The Aclass Akeyword Abegins Awith Aa Aclass Adeclaration, Awhich Aenables
Ayou Ato Adescribe Aa Aconstructor Aand Aa Aset Aof Amethods Ain Aa Aparticular
Afunctions Ato Abe Aincluded Ain Athe Aprototype. AWhen Ayou Ause Aan
Aconstructor Aas Aa Avalue. AYou Acan Aexclude Athe Aclass Aname Ain Aan
Aexpression.
Aassigned Aproperty Awith Asimilar Anames Ain Athe Aprototype, Ait Adoes Anot
Rabbit.prototype.teeth A= A"small";
console.log(killerRabbit.teeth);
// A→ Asmall
killerRabbit.teeth A= A"long, Asharp, Aand Abloody";
console.log(killerRabbit.teeth);
// A→ Along, Asharp, Aand Abloody
console.log(blackRabbit.teeth);
// A→ Asmall
console.log(Rabbit.prototype.teeth);
// A→ Asmall
Overriding Aproperties Ais Aused Ato Abestow Aa Astandard Afunction Aand Aarray
Aprototypes Aa Aseparate AtoString Amethod.
console.log(Array.prototype.toString A==
Object.prototype.toString);
// A→ Afalse
console.log([1, A3].toString());
// A→ A1,3
Calling AtoString Aon Aan Aarray Abestows Aa Aresult Athat Ais Alikened Ato
Acalling. Ajoin(",") Aon Ait. AIt Aplaces Aa Acomma Abetween Athe Avalues
Acontained Ain Athe Aarray. AIf Ayou Acall AObject.prototype.toString Ato Aan
console.log(Object.prototype.toString.call([1, A3]));
// A→ A[object AArray]
Maps
A Amap Ais Aa Adata Alayout Athat Aconnect Avalues Awith Aother Avalues. AIf Ayou
Awant Ato Amap Anames Ato Aages, Awrite Ayour Acode Alike Athis:
let Aages A= A{
Boris: A39,
Liang: A22,
Júlia: A62
};
console.log(`Julia Ais A${ages["Julia"]}`);
// A→ AJulia Ais A62
console.log("Is AJack's Aage Aknown?", A"Jack" Ain Aages);
// A→ AIs AJack's Aage Aknown? Afalse
console.log("Is AtoString's Aage Aknown?", A"toString" Ain Aages);
// A→ AIs AtoString's Aage Aknown? Atrue
Object Aproperty Anames Ashould Abe Ain Astrings. AIf Ayou Awant Aa Amap Athat
Athe Akeys Acannot Abe Atransformed Ato Astrings, Ado Anot Ause Aobject Aas Aa
Amap. AJavaScript Acontains Aa Aclass Acalled Amap. AIt Asaves Amapping Aand
Aenables Adifferent Atypes Aof Akeys.
console.log({x: A1}.hasOwnProperty("x"));
// A→ Atrue
console.log({x: A1}.hasOwnProperty("toString"));
// A→ Afalse
Polymorphism
When Aa Astring Afunction Ais Acalled Aon Aan Aobject, Athe AtoString Amethod Ais
Acalled Aon Athat Aobject Ain Aorder Ato Abuild Aa Asignificant Astring Afrom Ait.
AYou Acan Adefine Ayour Aown Aversion Aof AtoString Aso Athat Athey Acan Abuild
Aa Astring Athat Aconsists Aof Amore Aimportant Adata Amore Athan Athe A"[object
Rabbit.prototype.toString A= Afunction() A{
return A`a A${this.type} Arabbit`;
};
console.log(String(blackRabbit));
// A→ Aa Ablack Arabbit
When Aa Aline Aof Acode Ais Awritten Ato Afunction Awith Aobjects Athat Ahave Aa
Aspecific Alayout Ais Acalled Apolymorphism. APolymorphic Acode Acan
Afunction Aproperly Awith Avalues Aof Aseveral Ashapes.
Symbols
Symbols Aare Aunique Avalues Abuilt Awith Athe ASymbol Afunction Awhich
Acannot Abe Abuilt Atwice.
Aproperty Aname. ABelow Awe Arefer Ato Aa Abinding Astoring Athe Asymbol.
let AstringObject A= A{
[toStringSymbol]() A{ Areturn A"a Ajute Arope"; A}
};
console.log(stringObject[toStringSymbol]());
// A→ Aa Ajute Aropes
The Aiterator Ainterfaces
The Astated Aobject Ato Aa Afor/of Aloop Ais Aiterable. AThis Ameans Athat Ait
Aconsists Aof Aa Anamed Amethod Awith Athe ASymbol.iterator Asymbol Awhen
Athe Aproperty Ais Acalled. AThe Amethod Asends Aback Aan Aobject Athat
Aproduces Aa Asecond Alayout. AIt Acontains Aa Anext Amethod Athat Asends Aback
Athe Afollowing Aresult. AThe Aresult Ais Aan Aobject Awith Aa Avalue Aproperty
Athat Aproduces Athe Anext Avalue, ASymbol.iterator Acan Abe Aadded Ato Aseveral
ABelow Ais Athe Alayout Aof Aobjects Awith Ax, Ay, Aand Avalue Aproperties.
class AMatrixIterator A{
constructor(matrix) A{
this.x A= A0;
this.y A= A0;
this.matrix A= Amatrix;
}
next() A{
if A(this.y A== Athis.matrix.height) Areturn A{done: Atrue};
let Avalue A= A{x: Athis.x,
y: Athis.y,
value: Athis.matrix.get(this.x, Athis.y)};
this.x++;
if A(this.x A== Athis.matrix.width) A{
this.x A= A0;
this.y++;
}
return A{value, Adone: Afalse};
}
}
Below Ais Aan Aillustration Aof Aan Aiterable AMatrix Aclass:
Matrix.prototype[Symbol.iterator] A= Afunction() A{
return Anew AMatrixIterator(this);
};
We Acan Anow Aloop Aover Aa Amatrix Awith Afor/of.
let Amatrix A= Anew AMatrix(2, A2, A(x, Ay) A=> A`value A${x},${y}`);
for A(let A{x, Ay, Avalue} Aof Amatrix) A{
console.log(x, Ay, Avalue);
}
// A→ A0 A0 Avalue A0,0
// A→ A1 A0 Avalue A1,0
// A→ A0 A1 Avalue A0,1
// A→ A1 A1 Avalue A1,1
Layouts Aconsists Aof Amethods, Abut Ayou Acan Aalso Aattach Aproperties Athat
Astores Avalues Awith Ano Afunction Alike Aa AMap Aobject Acontaining Aa Asize
Aproperty Athat Adetermines Ahow Amany Akeys Aare Asaved Ain Athem. AAn
Aassessed Aproperty Acan Ahide Aa Amethod Acall. AThose Amethods Aare Acalled
Agetters, Aand Ayou Acan Adescribe Athem Aby Awriting A"get" Aat Athe Abeginning
Aof Athe Amethod Aname Ain Aa Aclass Adeclaration Aor Aan Aobject Aexpression.
let AvaryingSize A= A{
get Asize() A{
return AMath.floor(Math.random() A* A100);
}
};
console.log(varyingSize.size);
// A→ A73
console.log(varyingSize.size);
// A→ A49
When Ayou Awant Ato Ause Aan Aobject’s Asize Aproperty, Ayou Acall Athe
Aassociated Amethod. AYou Acan Aperform Aa Asimilar Atask Awhen Ayou Aa
class ATemperature A{
constructor(celsius) A{
this.celsius A= Acelsius;
}
get Afahrenheit() A{
return Athis.celsius A* A1.8 A+ A32;
}
set Afahrenheit(value) A{
this.celsius A= A(value A- A32) A/ A1.8;
}
static AfromFahrenheit(value) A{
return Anew ATemperature((value A- A32) A/ A1.8);
}
}
let Atemp A= Anew ATemperature(22);
console.log(temp.fahrenheit);
// A→ A71.6
temp.fahrenheit A= A86;
console.log(temp.celsius);
// A→ A30
The ATemperature Aclass Aenable Ayou Ato Aread Aand Awrite Athe Atemperature
Ain Adegrees ACelsius Aor Adegrees AFahrenheit, Abut Ait Asaves Aonly ACelsius
Aand Atransform Ato Aand Afrom ACelsius Ain Athe AFahrenheit Agetter Aand
Abefore Atheir Anames Aare Asaved Aon Athe Aconstructor. ASo, Ayou Acan Awrite
Adegrees AFahrenheit.
Inheritance
Some Amatrices Aare Asymmetric. AWhen Ayou Areflect Aa Asymmetric Amatrix
Athrough Aits Atop-left-to-bottom-right Adiagonal, Ait Adoes Anot Achange. AThe
Avalue Aset Aat Ax,y Aremains Athe Asame Aas Athe Ay,x. AUse Athe AJavaScript’s
Aprototype Asystem Ato Abuild Aa Anew Aclass, Athe Aprototype Afor Athe Anew
Aclass Ais Aacquired Afrom Athe Aold Aprototype Abut Asets Aa Anew Adefinition
console.log(
new ASymmetricMatrix(2) Ainstanceof ASymmetricMatrix);
// A→ Atrue
console.log(new ASymmetricMatrix(2) Ainstanceof AMatrix);
// A→ Atrue
console.log(new AMatrix(2, A2) Ainstanceof ASymmetricMatrix);
// A→ Afalse
console.log([1] Ainstanceof AArray);
// A→ Atrue
The Aoperator Awill Ascan Athrough Athe Atypes Athat Awere Ainherited.
ATherefore, Aa ASymmetricMatrix Ais Aan Ainstance Aof AMatrix. AYou Acan Aalso
Summary
In Athis Achapter, Awe Arealize Athat Aobjects Acan Aperform Amore Athan
Aholding Atheir Aproperties. AThey Aconsist Aof Aprototypes Athat Aare Aobjects
Aas Awell. AThey Abehave Alike Athey Acontain Aproperties Athat Athey Ado Anot
Aprovide Aas Along Aas Athe Aprototype Ahas Athat Aproperty. ASimple Aobjects
Ause Athe AObject. APrototype Ato Arepresent Atheir Aprototype. AWe Atalked
Aabout AConstructors Atoo, Aand Athey Aare Afunctions Athat Atheir Anames Aoften
Abegin Awith Aa Acapital Aletter, Aand Aused Awith Athe Anew Aoperator Ato Abuild
Anew Aobjects. AThe Anew Aobject’s Aprototype Awould Abe Athe Aobject Awithin
Exercise
Write Aa AJavaScript Aprogram Ato Acreate Aan Aarray, Athrough Aan Aiterator
Afunction Aand Aa Aprimary Aseed Avalue.
Solution
Chapter 8: A Robot
In Athis Achapter, Athere Awill Abe Awork Aon Aa Arobot Aprogram, Aa Ashort
Aprogram Athat Aexecutes Aa Atask Ain Aa Avirtual Aworld. AWe Awill Abe Ausing
Meadow Afield
The AMeadow Afield Avillage Ais Aa Asmall Aone Aconsisting Aof A14 Aroads Aand
A11 Aplaces. ABelow Ais Aan Aillustration Awith Aan Aarray Aof Aroads:
const Aroads A= A[
"Alice's AHouse-Bob's AHouse", A"Alice's AHouse-Cabin",
"Alice's AHouse-Post AOffice", A"Bob's AHouse-Town AHall",
"Daria's AHouse-Ernie's AHouse", A"Daria's AHouse-Town AHall",
"Ernie's AHouse-Grete's AHouse", A"Grete's AHouse-Farm",
"Grete's AHouse-Shop", A"Marketplace-Farm",
"Marketplace-Post AOffice", A"Marketplace-Shop",
"Marketplace-Town AHall", A"Shop-Town AHall"
]
The Avillage Aroads Acreate Aa Agraph. AA Agraph Ais Aa Agroup Aof Apoints
A(village Aplaces) Awith Alines Adividing Athem A(roads). AThe Agraph Ais Athe
Aworld Awhere Aour Arobot Awalks Athrough. ANow Alet Aus Atransform Aour Alist
Ato Aa Adata Alayout Athat Aeach Aplace Adecides Awhere Ayou Acan.
function AbuildGraph(border) A{
let Agraph A= AObject.create(Null);
function AaddBorders(from, Ato) A{
if A(graph[from] A== Anull) A{
graph[from] A= A[to];
} Aelse A{
graph[from].push(to);
}
}
for A(let A[from, Ato] Aof Aborders.map(r A=> Ar.split("-"))) A{
addEdge(from, Ato);
addEdge(to, Afrom);
}
return Agraph;
}
const AroadGraph A= AbuildGraph(roads);
Given Aan Aarray Aof Aedges, AbuildGraph Acreates Aa Amap Aobject Athat, Afor
Aeach Anode, Asaves Aa Aconnected Anode Aof Aan Aarray. AIt Autilizes Athe Asplit
Amethod Ato Ago Athrough Athe Aroad Astrings, Athat Acontains Athe Aform
A"Start-End", Ato Atwo-element Aarrays Awithin Athe Astart Aand Aend Aas
Adifferent Astrings.
The task
A
Our Arobot Awill Awalk Aacross Athe Avillage. AThere Aare Adifferent Aparcels Ain
Aseveral Aplaces. AThe Arobot Areceives Aa Aparcel Awhen Ait Aarrives Aand
Adelivers Athem Awhen Ait Agets Ato Atheir Adestination. AWhen Aall Atasks Ahave
Aall Abeen Aexecuted, Aall Aparcels Amust Abe Adelivered. AIf Ayou Awant Ato
Areplicate Athis Aprocess, Adescribe Aa Avirtual Aworld Athat Acan Adefine Ait.
AThis Amethod Areveals Athe Arobot Alocation Aas Awell Aas Athe Aparcels. ALet
Aus Abring Adown Athe Avillage's Astate Ato Aa Aspecific Aset Aof Avalues Athat
Adescribes Ait.
class AVillageState A{
constructor(place, Aparcels) A{
this.place A= Aplace;
this.parcels A= Aparcels;
}
move(destination) A{
if A(!roadGraph[this.place].includes(destination)) A{
return Athis;
} Aelse A{
let Aparcels A= Athis.parcels.map(p A=> A{
if A(p.place A!= Athis.place) Areturn Ap;
return A{place: Adestination, Aaddress: Ap.address};
}).filter(p A=> Ap.place A!= Ap.address);
return Anew AVillageState(destination, Aparcels);
}
}
}
The Amove Amethod Ais Athe Aaction Acenter. AIt Aexamines Aif Aa Apath Aleads
Afrom Aone Aplace Ato Aanother, Aif Anot Athe Aold Astate Ais Areturned Abecause
Athe Amove Ais Anot Avalid. AThen Ait Abuilds Aa Anew Astate Awith Athe
Adestination Aas Athe Arobot’s Anew Aplace Aas Awell Aas Abuilding Aa Anew Aset
Aof Aparcels. AThe Acall Ato Amap Ahandles Athe Amovement Aand Athe Acall Ato
Aan Aobject Aso Athat Ait Adisregards Awriting Ato Aits Aproperties.
Atakes Aa AVillageState Aobject Aand Asend Aback Athe Aname Aof Aa Anearby
Aplace. AThe Arobot Asends Aback Aan Aobject Aconsisting Aof Aits Aintended
Adirection Aand Aa Amemory Avalue Areturned Athe Anext Atime Ayou Acall Ait.
Aan Aexample:
function ArandomPick(array) A{
let Achoice A= AMath.floor(Math.random() A* Aarray.length);
return Aarray[choice];
}
function ArandomRobot(state) A{
return A{direction: ArandomPick(roadGraph[state.place])};
}
If Ayou Awant Ato Atest Athis Aunique Arobot, Acreate Aa Anew Astate Awith Afew
Aparcels.
Pathfinding
An Ainteresting Aapproach Ais Ato Adevelop Aroutes Afrom Athe Astarting Apoint,
Aand Ainspect Aevery Aavailable Aplace Athat Ahas Anot Abeen Avisited Auntil Ait
Aroute Aand Athe Astart Aposition. AEvery Alocation Acan Abe Atouched Afrom
Aevery Alocation, Aa Aroute Acan Aalso Abe Afound Abetween Atwo Apoints
Aplaces Aand A14 Aroads, Athen Awe Amoved Ato Athe APersistent Adata,
Awhereby AData Astructures Abehave Alike Astrings Aand Ado Anot Achange.
ASimulation Aenables Ayou Ato Apass Amemory Ato Arobots Aand Alet Athem Ato
Exercise
Write Aa Astraightforward AJavaScript Aprogram Ato Aconnect Aevery Aelement
Ain Athe Abelow Aarray Ainto Aa Astring.
A
Chapter 9: Bugs and Errors
Computer Aprogram Aflaws Aare Acalled Abugs, Athe Amistakes Amade Aby Aa
Acomputer Aprogram.
Strict Amode
You Acan Autilize AJavaScript Ain Aa Astrict Amode. AThis Acan Abe Aachieved Aby
Aadding Athe Astring A"use Astrict" Aright Aat Athe Atop Aof Aa Afile. ABelow Ais Aan
Aillustration:
function AcanYouSpotTheProblem() A{
"use Astrict";
for A(counter A= A0; Acounter A< A10; Acounter++) A{
console.log("Happy Ahappy");
}
}
canYouSpotTheProblem();
// A→ AReferenceError: Acounter Ais Anot Adefined
Use Athe Abelow Acode Awhen Ayou Awant Ato Acall Aa Aconstructor Afunction
Aomitting Athe Anew Akeyword Aso Ait Adoesn’t Aintroduce Aa Anewly Acreated
Aobject:
Ayou Acall Athem Awithout Athe Anew. AStrict Amode Adoes Anot Aallow Asetting
Alanguage Aproblems.
Testing
You Acan Ause Athis Afunction Ato Asearch Afor Amistakes Ain Aa Aprogram. AIt Ais
Aachieved Aby Arunning Aover Aand Aover Aagain. AThe Acomputer Ais Aexcellent
Ain Arepetitive Atasks. AThis Aprocess Ais Acalled AAutomated Atesting. AIt
Ago Athrough Asmall Alabeled Aprograms Athat Adetermine Asome Apart Aof Ayour
Acode. AFor Ainstance, Alet Aus Acreate Aa Aset Aof Atests Afor Athe AtoUpperCase
Aparticular Aline Ain Ayour Ablock Aof Acode. ATake Aa Alook Aat Athe Aerror
Adescription, Aand Ayou Awill Afind Athe Aproblematic Aline. AThis Aexample
Aprogram Atries Ato Atransform Aa Awhole Anumber Ainto Aa Astring Awithin Aset
Abase A(decimal, Abinary, Aand Aso Aon) Aby Acontinuously Aselecting Aout Athe
Alast Adigit Aand Adividing Athe Anumber Ato Aeradicate Athe Adigit.
Aexecution. AWe Awant An Ato Aget Athe Avalues A13, A1, Aand Athen A0. AWe
Ashould Astate Athe Avalue Aat Athe Abeginning Aof Athe Aloop'
13
1.3
0.13
0.013…
1.5e-323
Dividing A13 Aby Awill Anot Aprovide Aa Awhole Anumber. AInstead Aof An A/=
Abase, Awhat Awe Awant Ais An A= AMath.floor(n A/ Abase) Aso Athat Athe Anumber
Another Agreat Away Ato Aspecify Aa Abreakpoint Ais Ato Ainsert Aa Adebugger
Astatement A(keyword) Ainto Ayour Aprogram. AIf Athe Adeveloper Atools Awill
Apick Ait Aup Aand Apause Athe Aprogram Aanytime Ait Agets Ato Athat Astatement
Exceptions
When Aa Afunction Ais Acannot Ago Afurther, Athe Aexception Ahandling Ais Athe
Aplace Athat Aunderstands Ahow Ato Afix Athat Aproblem. AExceptions Aare Aa
Atool Athat Aenables Aa Ablock Aof Acode Athat Aruns Ainto Adifficulties Ato Athrow
Aan Aexception, Aand Aan Aexception Acould Abe Aany Avalue. AYou Acan
Aintentionally Acreate Aproblems Awithin Ayour Aline Aof Acode Ato Acatch Athe
Aexception Aduring Aits Azooming Adown Athe Aprocess Aso Athat Ayou Acan Ause
Ait Ato Asolve Athe Aproblem Aand Acontinue Ayour Awork.
Aexception Acould Astop Athem Afrom Ahappening. ABelow Ais Asome Abad
Abanking Acode:
const Aaccounts A= A{
a: A100,
b: A0,
c: A20
};
function AgetAccount() A{
let AaccountName A= Aprompt("Enter Aan Aaccount Aname");
if A(!accounts.hasOwnProperty(accountName)) A{
throw Anew AError(`No Asuch Aaccount: A${accountName}`);
}
return AaccountName;
}
function Atransfer(from, Aamount) A{
if A(accounts[from] A< Aamount) Areturn;
accounts[from] A-= Aamount;
accounts[getAccount()] A+= Aamount;
}
The Atransfer Afunction Ais Aused Ato Asend Aa Aparticular Asum Aof Amoney
Afrom Aa Astated Aaccount Ato Aanother, Aas Awell Aas Ademanding Afor Athe
Aname Aof Athe Aother Aaccount. AIf Ayou Ainput Aa Awrong Aaccount Aname,
AgetAccount Aoutputs Aan Aexception. AIf Athe Amoney Ahas Abeen Asent Afrom
Athe Afirst Aaccount Aand Athen Athe Aprogram Aoutputs Aan Aexception Abefore
Ait Agets Athe Amoney Atransferred Ainto Athe Aother Aaccount, Athe Amoney Awill
Adisappear. ASolve Athis Aproblem, Autilize Athe Afinally Ablock Acode. AThis Ais
Aillustrated Abelow:
Aas Ainspecting Aup Aa Aproperty Aon Anull, Aciting Aa Anonexistent Abinding, Aor
Acatching Aof Aexceptions. ABelow Ais Aan Aexample Athat Atries Ato Acontinue
for A(;;) A{
try A{
let Adir A= ApromtDirection("Where?"); A// A← Atypo!
console.log("You Aselect A", Adir);
break;
} Acatch A(e) A{
console.log("Invalid Adirection. ATry Aagain.");
}
}
The Afor A(;;) Aconstruct Ais Aused Ato Abuild Aa Aloop Athat Acannot Abe
Aterminated Aon Aits Aown. AYou Acan Abreak Aout Aof Athe Aloop Awhen Ayou
Aan Aundefined Avariable Aerror. AIf Ayou Awant Ato Acatch Aa Aparticular Atype
Aof Aexception, Acheck Ain Athe Acatch Ablock Aif Athe Aexception Ayou Ahave Ais
Athe Asame Aone Ayou Awant Aand Arethrow Ait. ALet’s Adescribe Aa Anew Akind
Alocally. AYou Acan Atrack Athem Awith Aspecial Areturn Avalues Aor Athe Ause Aof
Aexceptions.
Exercise
Fix Athe Abelow Abroken Acode Aand Aindicate Athe Aproblem:
for(var AI=0; AI A> A5; AI++){
console.log(i)
}
Solution
Problem A– AIncorrect Acondition Awithin Athe Aloop.
Chapter 10: Regular Expressions
Programming Aconcepts Aand Atools Aadvances Ain Aa Adisorganized Away, Abut
Aits Asuccess Aindicates Aan Aexcellent Apiece Aof Atechnology. AIn Athis
Achapter, Awe Awill Adiscuss Aregular Aexpressions. AThey Aare Aused Ato
Alanguage Athat Aconsists Aof AJavaScript Aas Awell Aas Aother Alanguages.
A(/) Acharacters.
Aif Aa Astring Acontains Aa Amatch Aof Athe Apattern Ain Athe Aexpression.
console.log(/abc/.test("abcde"));
// A→ Atrue
console.log(/abc/.test("abxde"));
// A→ Afalse
A Aregular Aexpression Acontains Aonly Anon-special Acharacters Athat
Arepresent Athe Asuccession Aof Acharacters. AIf AABC Apapers Aanywhere Ain
Athe Astring, Awe Aare Arunning Aa Atest, Athe Atest Awill Areturn Atrue.
Sets of characters
A A
Regular Aexpressions Aenable Aus Ato Aindicate Adifficult Apatterns. AIf Ayou
Awant Ato Aequal Aany Anumber Ain Aa Aregular Aexpression, Aplace Aa Aset Aof
console.log(/[0123456789]/.test("in A1992"));
// A→ Atrue
console.log(/[0-9]/.test("in A1992"));
// A→ Atrue
A Ahyphen A(-) Abetween Atwo Acharacters Ainside Asquare Abrackets Ais Aused
Ato Aspecify Aa Ascope Aof Acharacters Athat Athe Acharacter’s AUnicode
Anumbers Adecide Athe Acommand. ACharacters Afrom A0 Ato A9 Astay Anext Ato
Aeach Aother Ain Athis Acommand A(codes A48 Ato A57), Aso A[0-9] Awrapping
Athem Aall Aand Aequals Aany Adigit. ACommon Acharacter Agroups Acontain
Digits Aare Apart Aof Athem: A\d Ameans Athe Aexact Athing Aas A[0-9].
\d AAny Acharacter Adigit
\w Aan Aalphanumeric Acharacter A(“word Acharacter”)
\s AAny Awhitespace Acharacter A(space, Atabs, Anewline, Aand Asimilar)
\D AA Acharacter Awhich Ais Anot Aa Adigit
\W AA Anonalphanumeric Acharacter
\S AA Anonwhitespace Acharacter
. AAny Acharacter Awithout Afor Anewline
So, Amatch Aa Adate Aand Atime Aformat Alike A01-30-2003 A15:20 Awith Athe
Aexpression Abelow:
Aperiod Ahas Ano Ameaning Aas Awell Aas Aother Aunique Acharacters, Alike A+.
let AnotBinary A= A/[^01]/;
console.log(notBinary.test("1100100010100110"));
// A→ Afalse
console.log(notBinary.test("1100100010200110"));
// A→ Atrue
Repeating Aparts Aof Aa Apattern
Now Ayou Ahave Aa Aperfect Aunderstanding Aof Ahow Ato Aequal Asingle
Adigits. AIf Ayou Awant Ato Amatch Aa Awhole Anumber Aor Aa Asuccession Aof
Amore Adigits, Aplace Aa Aplus Asign A(+) Aimmediately Aafter Aan Ainput Ain Aa
Aregular Aexpression. AThis Asignifies Athat Athe Aelement Acould Arepeat Amore
console.log(/'\d+'/.test("'123'"));
// A→ Atrue
console.log(/'\d+'/.test("''"));
// A→ Afalse
console.log(/'\d*'/.test("'123'"));
// A→ Atrue
console.log(/'\d*'/.test("''"));
// A→ Atrue
The Astar A(*) Aenables Athe Apattern Ato Aequal Azero. AIn Athe Abelow
Aexample, Athe Au Acharacter Ais Apermitted Ato Arise, Abut Athe Apattern Ais Aalso
{2,4} Asignifies Athat Athe Aelement Amust Aexecute Atwice Aand Aat Amost Afour
Atimes. ABelow Ais Aanother Apattern Aof Athe Adate Aand Atime Apattern, Awhich
Aexpression, Awhich Ais Acited Ain Aparentheses, Acounts Aas Aan Aindividual
Aone Aor Amore Asuccessions Alike Athat. AThe AI Aending Athe Afirst Aexpression
Ain Athe Aexample Ais Aused Afor Acase Ainsensitive, Aenabling Ait Ato Aequal Athe
Auppercase AB Ain Athe Ainput Astring. AAlthough Athe Apattern Ais Aentirely
Alowercase.
A(execute) Amethod, Awhich Awill Aoutput Anull Aif Ait Acould Afind Aa Amatch
Aand Asends Aback Aan Aobject Acontaining Adata Aabout Athe Amatch.
Amatch Ais Athe Afirst, Athe Anext Ais Athe Amatched Apart Aby Athe Afirst Agroup,
Aseveral Atimes, Aonly Athe Alast Amatch Afinishes Ain Athe Aarray.
console.log(/bad(ly)?/.exec("bad"));
// A→ A["bad", Aundefined]
console.log(/(\d)+/.exec("123"));
// A→ A["123", A"3"]
The ADate Aclasses
JavaScript Aconsists Aof Aa Astandard Aclass Athat Arepresent Adates; Ait Ais
Acalled ADate. AIf Ayou Awant Ato Abuild Aa Adate Aobject Autilizing Anew, Afirst
console.log(new ADate());
// A→ AMon ANov A11 A2018 A16:19:11 AGMT+0100 A(CET)
You Acan Aas Awell ABuild Aan Aobject Afor Aa Aparticular Atime.
console.log(new ADate(2008, A11, A8));
// A→ AWed ADec A08 A2008 A00:00:00 AGMT+0100 A(CET)
console.log(new ADate(2008, A11, A9, A12, A59, A59, A999));
// A→ AWed ADec A08 A2009 A12:59:59 AGMT+0100 A(CET)
You Ashould Afollow Athe AJavaScript Adate Anaming Aconvention Awhere
Amonth Anumbers Abegin Aat Azero A(therefore, ADecember Ais A11), Aand Aday
Acount Aby Abuilding Aa Anew ADate Aobject Aand Acall AgetTime Aon Ait Aor Ause
function AgetDate(strings) A{
let A[_, Amonth, Aday, Ayear] A=
/(\d{01,2})-(\d{01,2})-(\d{4})/.exec(strings);
return Athe Anew ADate(year, Amonth A- A1, Aday);
}
console.log(getDate("1-30-2003"));
// A→ AThursday AJan A30 A2003 A00:00:00 AGMT+0100 A(CET)
String and Word boundaries
A A A
You Acan Aensure Athat Athe Amatch Aspans Athe Aentire Astring Aby Aadding Athe
Amarkers A^ Aand A$. AThe Acaret Aequals Athe Abeginning Aof Athe Ainput
Astring, Awhile Athe Adollar Asign Aequals Athe Aend. ATherefore, A/^\d+$/
Aany Astring Athat Abegins Awith Aan Aexclamation Amark, Aand A/x^/ Amatches
Ano Astring. AIf Ayou Awant Athe Adate Ato Abegin Aand Aend Aon Aa Aboundary
Aword, Autilize Athe Amarker A\b. AA Aword Aboundary Acan Abegin Aor Afinish
console.log(/cat/.test("concatenate"));
// A→ Atrue
console.log(/\bcat\b/.test("concatenate"));
// A→ Afalse
Choice Apatterns
If Ayou Awant Ato Aknow Aif Achunk Aof Atext Aconsists Aof Aa Anumber Aand
Afollowed Aby Awords Asuch Aas Achicken, Apig, Aor Acow, Alet’s Awrite Athree
Aregular Aexpressions Aand Atest Athem. AWe Autilize Athe Apipe Acharacter A(|)
Ato Amark Aa Achoice Abetween Athe Apattern Ato Athe Aleft Aand Aright:
console.log("papa".replace("p", A"m"));
// A→ Amapa
The Afirst Aargument Acan Aequally Abe Aa Aregular Aexpression. AIt Areplaces
Athe Afirst Amatch Aof Athe Aregular Aexpression. AIf Ayou Aadd Aa Ag Aoption
A(for Aglobal) Ato Athe Aexpression, Aevery Amatch Acontained Ain Athe Astring
console.log("Borobudur".replace(/[ou]/, A"a"));
// A→ ABarobudur
console.log("Borobudur".replace(/[ou]/g, A"a"));
// A→ ABarabadar
The Amajor Aimportance Aof Autilizing Aregular Aexpressions Awith Areplace Ais
Abecause Ait Arefers Ato Amatched Agroups Ain Athe Areplacement Astring. AFor
Ainstance, Ayou Ahave Aa Alarge Astring Aconsisting Aof Apeople's Aname, Aone
Aname Aper Aline, Ausing Athe Aformat ALastname, AFirstname. AYou Acan Aas
Awell Achange Aand Aremove Athese Anames, Aeradicate Athe Acomma Ato Aget Aa
console.log(
"Liskov, ABarbara\nMcCarthy, AJohn\nWadler, APhilip"
.replace(/(\w+), A(\w+)/g, A"$2 A$1"));
// A→ ABarbara ALiskov
// AJohn AMcCarthy
// APhilip AOaks
The A$1 Aand A$2 Acontained Ain Athe Areplacement Astring Arefer Ato Athe
Agroup Ain Aparentheses Ain Apattern. A$1 Ais Arestored Aby Athe Atext, Awhich
Amatches Aopposing Athe Afirst Agroup, A$2 Aup Ato A$9. AEach Aand Aevery
Amatch Acan Abe Areferred Ato Awith A$&. AYou Acan Apass Aa Afunction Ainstead
Aof Aa Astring Aas Athe Asecond Aargument Ato Arestore. AFor Aeach
Areplacement, Athe Afunction Ais Acalled Awith Athe Amatched Agroup Aas
Aarguments, Aand Ainclude Athe Areturn Avalue Ainto Athe Anew Astring. ABelow
function AstripComments(code) A{
return Acode.replace(/\/\/.*|\/\*[^]*\*\//g, A"");
}
console.log(stripComments("1 A+ A/* A2 A*/3"));
// A→ A1 A+ A3
155
console.log(stripComments("x A= A10;// Aten!"));
// A→ Ax A= A10;
console.log(stripComments("1 A/* Aa A*/+/* Ab A*/ A1"));
// A→ A1 A1
The Asection Abefore Athe Aor Aoperator Amatches Atwo Aslash Acharacters
Aaccompanied Awith Aany Anumber Athat Ais Anot Aa Anewline Acharacter. AUse
Repetitive AOperators A(+, A*, Aand A{}) Aare Acalled Agreedy Awhich Ameans
Athey Amatch Aand Athey Acan Aalso Abacktrack. AIf Aa Aquestion Amark Ais
Aplaced Aafter Athem A(+?, A*?, A??, A{}?), Athe Agreed Adisappears Aand Abegin
Amatching Ano Amatter Ahow Asmall. AThe Asmallest Astretch Aof Acharacters
Awhich Abrings Aa A*/, Ais Athe Astar Amatch. AIt Aabsorbs Aone Ablock Acomment
Aonly.
function AstripComments(code) A{
return Acode.replace(/\/\/.*|\/\*[^]*?\*\//g, A"");
}
console.log(stripComments("1 A/* Aa A*/+/* Ab A*/ A1"));
// A→ A1 A+ A1
There Aare Afew Ainstances Awhereby Ayou Awould Anot Aunderstand Athe
Aspecific Apattern Ayou Ashould Amatch Aagainst Awhen Ayour Acode Ais Abeing
Awritten. AIf Ayou Awant Ato Asearch Afor Athe Auser Aname Ain Aa Achunk Aof
Atext Aand Awrap Ait Ain Aunderscore Acharacters Ato Amake Ait Aunique. ABut
Ayou Acan Acreate Aa Astring Aand Autilize Athe ARegExp Aconstructor. ABelow
Ais Aan Aexample:
Asecond Aargument Ato Athe ARegExp Aconstructor Aconsists Aof Athe Aoptions
Aneeded Afor Athe Aregular Aexpression. AThe A"gi" Arepresent Aglobal Aand
AindexOf Amethod Aa Aregular Aexpression, Aand Ait Asends Aback Athe Afirst
Aindex Awhere Ait Asees Athe Aexpression Aand Awhen Ait Adoes Anot Asee Ait
Areturns A-1.
console.log(" Aword".search(/\S/));
// A→ A2
console.log(" A".search(/\S/));
// A→ A-1
The AlastIndex Aproperty
The Aexec Amethod Adoes Anot Aproduce Aa Abetter Away Ato Abegin Athe Asearch
Afrom Aa Astated Aposition Ain Athe Astring. ARegular Aexpression Aobjects
Acontain Aproperties. AOne Aof Athe Aproperties Ais Acalled Asource, Aand Ait
Aconsists Aof Athe Astring Athat Abuilt Athe Aexpression. AlastIndex Ais Aanother
Aproperty, Aand Ait Ainfluences Asome Asmall Acircumstances, Awhere Athe Anext
Amatch Awill Abegin. AThe Aregular Aexpression Amust Acontain Athe Aglobal
A(g) Aor Asticky A(y) Aoption, Aand Athe Amatch Awill Aoccur Ausing Athe Aexec
AlastIndex, Awhile Awith Aglobal, Ait Awill Afind Aa Aposition Afor Athe Amatch
Acan Abegin.
AlastIndex Aproperty.
Let Ainput A= A"A Astring Awith Athree Anumbers Ain Ait... A42 Aand A88.";
let Anumber A= A/\b\d+\b/g;
let Amatch;
while A(match A= Anumber.exec(input)) A{
console.log("Found", Amatch[0], A"at", Amatch.index);
}
// A→ AFound A3 Aat A14
// AFound A42 Aat A33
// AFound A88 Aat A40
The Aassignment Aexpression Avalue A(=) Ais Athe Aspecified Avalue. ASo
Autilizing Amatch A= Anumber.Exec A(input) Aas Athe Acondition Awithin Athe
Awhile Astatement, Athe Amatch Ais Awe Aexecute Athe Amatch Aat Athe
Abeginning Aof Aevery Aiteration, Astore Athe Aresult Awithin Aa Abinding, Aand
Asubobjects Afor Asections Aand Athe Afirst Asection Aheader. AThe Asubobjects
Alines. AThis Awill Amake Athe Asplit Amethod Aenable Aa Aregular Aexpression
Aas Aits Aargument, Ayou Acan Autilize Aa Aregular Aexpression Alike A/\r?\n/ Ato
Adivide Ait Ain Aa Away Athat Aenables Aboth A"\n" Aand A"\r\n" Abetween Alines.
function AparseINI(string) A{
// ABegin Awith Aan Aobject Ato Ahold Athe Atop-level Afields
let Aresult A= A{};
let Asection A= Aresult;
string.split(/\r?\n/).forEach(line A=> A{
let Amatch;
if A(match A= Aline.match(/^(\w+)=(.*)$/)) A{
section[match[1]] A= Amatch[2];
} Aelse Aif A(match A= Aline.match(/^\[(.*)\]$/)) A{
section A= Aresult[match[1]] A= A{};
} Aelse Aif A(! A/^\s*(;.*)?$/.test(line)) A{
throw Anew AError("Line A'" A+ Aline A+ A"' AInvalid.");
}
});
return Aresult;
}
console.log(parseINI(`
name=Vasilis
[address]
city=Tessaloniki`));
// A→ A{name: A"Vasilis", Aaddress: A{city: A"Tessaloniki"}}
The Acode Aadvances Aover Athe Afile Alines Aand Acreate Aup Aan Aobject.
AProperties Aat Athe Atop Aare Asaved Astraight Ainto Athat Aobject, Awhereby
Aproperties Acan Abe Afound Ain Asections Aare Asaved Ain Aa Adifferent Asection
Aobject. AThe Asection Abinding Apoints Ato Athe Aobject Afor Athe Aexisting
Asection. AThere Aare Atwo Atypes Aof Aimportant Alines—section Aheaders Aor
Aproperty Alines. AIf Aa Aline Ais Aalways Aproperty, Ait Ais Asaved Ain Athe
Aexisting Asection. AIf Ait Ais Aa Asection Aheader, Abuild Aa Anew Asection
The Apattern Aif A(match A= Astring.match()) Ais Aidentical Ato Athe Atrick Aof
Autilizing Aan Aassignment Aas Athe Acondition Afor Aa Awhile.
International Acharacters
Because Aof AJavaScript’s Ainitial Asimplistic Aimplementation Aand Athat Athis
Aapproach Aset Ain Astone Aas Astandard Abehavior, AJavaScript’s Aregular
Aexpressions Aare Asomewhat Adumb Aabout Acharacters Athat Adon’t Ashow Ain
Aexpressions, Aa A“word Acharacter” Ais Ajust Aone Aof Athe A26 Acharacters Ain
Athe ALatin Aalphabet A(lowercase Aor Auppercase), Adecimal Adigits, Aand Athe
Athey Awill Anot Amatch A\w, Awill Amatch Auppercase A\W, Athe Anonword
Acategory. AIt Aindicates Athat Acharacters Acomposed Aof Atwo Acode Aunits.
Athat Acharacters Aproperly. AThe Awrong Abehavior Aremain Aset Aas Athe
You Acan Ause A\p Ain Aa Aregular Aexpression Ato Amatch Aevery Acharacter
Athat AUnicode Astandard Agives Aa Aset Aproperty.
console.log(/\p{Script=Greek}/u.test("α"));
// A→ Atrue
console.log(/\p{Script=Arabic}/u.test("α"));
// A→ Afalse
console.log(/\p{Alphabetic}/u.test("α"));
// A→ Atrue
console.log(/\p{Alphabetic}/u.test("!"));
// A→ Afalse
Unicode Adescribes Aa Anumber Aof Aimportant Aproperties, Aalthough
Asearching Afor Athe Aone Athat Ayou Aneed Amay Anot Aalways Abe Atrivial.
Summary
We Afocused Aon Aregular Aexpressions Ain Athis Achapter. AAn Aobject Athat
Astands Afor Apatterns Ain Astrings Ais Aa Aregular Aexpression. AThey Aconsist
Aof Atheir Alanguage Aand Autilize Ait Afor Athe Aexhibition Aof Athese Apatterns.
Afinds Aa Amatch, Ait Asends Aback Aan Aarray Ahaving Aevery Amatched Agroup.
AThis Atype Aof Aarray Acontains Aan Aindex Aproperty Athat Aspecifies Awhere
Athe Amatch Astarted. AStrings Ause Aa Amatching Amethod Ato Amatch Athem Ain
Aopposition Ato Aa Aregular Aexpression Aas Awell Aas Aa Asearch Amethod Ato
Afind Aone, Asending Aback Aonly Athe Abeginning Aposition Aof Athe Amatch.
Exercise
Write Aa AJavaScript Aprogram Aworking Aas Aa Atrim Afunction A(string)
Autilizing Aregular Aexpression.
Solution
Chapter 11: Modules
This Aparticular Aprogram Ahas Aa Asimple Alayout. AIt Ais Avery Ato Aexplain Aits
Aconcept, Aand Aevery Apart Aplays Aa Aprecise Arole. AThe Aorganizing Aand
Amaintaining Aof Athe Alayout Acan Abe Amuch Amore Awork, Abut Ait Aeventually
Apays Aoff Athe Anext Atime Asomeone Auses Athe Aprogram Ain Athe Afuture.
ATherefore, Ayou Acan Abe Aintrigued Ato Aneglect Ait Aand Aenable Athe Aprogram
Aparts Ato Abecome Aintermixed, Awhich Awill Acreate Atwo Apractical Aissues.
AFirst, Awhen Aeverything Atouches Aeverything Aelse, Ait Ais Ahard Ato Alook Aat
Aany Aset Apiece Ain Aseparation. AYou Awill Ahave Ato Acreate Aa Acomprehensive
Aunderstanding Aof Athe Afull Aprogram. ASecond, Ausing Aany Aof Athe
Modules
A AModule Ais Aa Atype Aof Aprogram Athat Adetermines Awhich Apieces Ait
Ashould Adepend Aon Aand Awhat Afunctionality Ashould Ait Aproduce Afor Aother
Apieces Ato Autilize. ABy Aputting Aa Alimit Aon Athe Aways Amodules
Acommunicate Awith Aone Aanother, Athe Asystem Ais Aidentical Ato ALEGO,
Alike Amud Awhere Athe Aentire Apieces Amix Awith Aeverything. ADependencies
Apiece Afrom Aanother Amodule, Ait Adepends Aon Athat Amodule. AThe Amodule
Aitself Aillustrates Athis Afact, Aand Ait Ais Aused Ato Adetermine Awhich Aother
Amodules Ashould Abe Aavailable Ato Ause Aa Astated Amodule Aand Ato Aload
Packages
One Asignificant Abenefit Aof Abuilding Aa Aprogram Aout Aof Adifferent Apieces,
Aand Abeing Aable Ato Alaunch Athose Apieces Aindividually, Ais Athat Ayou Acan
Aapply Athe Asame Apiece Ain Aseparate Aprograms. AWhen Athe Aduplication Aof
Acode Abegins, Athe Apackage Acomes Ain Ato Aplay. AA Apackage Ais Aa Ablock Aof
Acode Athat Acan Abe Ashared. AIt Amay Aconsist Aof Aone Aor Amore Amodules
Aand Aholds Ainformation Aabout Athe Aother Adepending Apackages. APackages
Aalso Acontain Adocumentation Adescribing Aits Afunctions Aso Athat Apeople
Awho Adidn’t Awrite Ait Acan Autilize Ait. AWhen Aa Aproblem Aoccurs Ain Aa
Aautomatically. ANow Aprograms Adepending Aon Aits Aupgrades Ato Athe Alatest
Aversion. AWorking Alike Athis, Ayou Aneed Ainfrastructure. AA Aplace Ato Asave
Aand Asearch Afor Apackages Aand Aan Aeasy Away Ato Aupgrade Aand Ainstall
Divide ANPM Ainto Atwo Athings: Aan Aonline Aservice Awhere Ayou Acan
Aupload Aand Adownload Apackages Aand Aa Aprogram Athat Ahandles Athe
Alicensed.
Improvised Amodules
Before Athe Ayear A2015, Athe AJavaScript Alanguage Adid Anot Acontain Aany
Abuilt-in Amodule Asystem, Abut Apeople Ahave Abeen Acreating Alarge Asystems
Ausing AJavaScript Afor Aover Aa Adecade Anow, Aand Athey Arequired Amodules.
AThey Adeveloped Atheir Amodule Asystems Aand Aintegrated Athem Ainto Athe
Alanguage. AYou Acan Autilize AJavaScript Afunctions Ato Abuild Alocal Ascopes
Aand Aobjects Ato Astand Afor Amodule Alayouts. AUse Athis Amodule Ato Aover
Abetween Aday Anames Aand Anumbers. AIts Alayout Acontains Athe AweekDay.
.number, Aand Ait Ahides Aits Alocal Abinding Anames Ainside Athe Ascope Aof Aa
Afunction
Aisolation Ato Aa Acertain Adegree, Abut Ait Adoes Anot Adeclare Adependencies.
AInstead, Ait Ajust Aputs Aits Ainterface Ainto Athe Aglobal Ascope Aand Aawaits Aits
There Aare Adifferent Atypes Aof Aways Ato Atake Adata Aand Alaunch Ait Aas Apart
Aof Athe Aexisting Aprogram. AThe Abest Away Ais Ato Autilize Athe Aunique
const Ax A= A1;
function AevalAndReturnX(code) A{
eval(code);
return Ax;
}
console.log(evalAndReturnX("var Ax A= A2"));
// A→ A2
console.log(x);
// A→ A1
A Aless Acomplicated Away Ato Aexplain Adata Aas Acode Ais Ausing Athe
AFunction Aconstructor. AIt Atakes Atwo Atypes Aof Aargument, Aa Astring Awith Aa
Acomma, Adivided Alist Aof Aargument Anames Aand Aa Astring Awith Athe Abody
Afunction. AIt Aencloses Athe Acode Ain Aa Afunction Avalue Ato Aenable Ait Aget
CommonJS
The Acommon Aapproach Aused Ato Agobble Aon AJavaScript Amodules Ais Athe
ACommonJS Amodules. ANode.js Auses Athis Aapproach Aas Awell Aas Aa Alot Aof
Apackages Aon ANPM. AThe Amajor Aidea Ain ACommonJS Amodules Ais Athe
Afunction Anamed Arequire. AIf Ayou Acall Athis Atogether Awith Athe
Adependency's Amodule Aname, Ait Aloads Athe Amodule Aand Asends Aback Aits
Ainterface. AThe Aloader Acovers Athe Amodule Acode Awithin Aa Afunction, Aand
Amodules Ahave Atheir Alocal Ascope Aautomatically. AYou Acan Acall Arequire Ato
Asee Atheir Adependencies Aand Aplace Atheir Alayout Ain Athe Aobject Athat
AIt Autilizes Atwo Apackages Afrom ANPM, Athe Aordinal Ato Atransform
Anumbers Ato Astrings Alike A"1st" Aand A"2nd", Aand Adate-names Ato Aobtain
Athe AEnglish Anames Afor Amonths Aand Aweekdays. AIt Aexports Aa Asingle
AThe Atemplate Astring Acould Acontain Acodes Athat Acontrol Athe Aformat, Asuch
Aas AYYYY A(the Afull Ayear) Aand ADo Afor Athe Aordinal Aday Aof Athe Amonth.
AYou Acan Aset Aa Astring Ato Ait Alike A"MMMM ADo AYYYY" Ato Aget Aoutput
Asuch Aas ANode.js Aand Athe Abrowser Aoffers Atheir Aunique Aways Aof
Aaccessing Afiles. AThe Aabove Aexample Abluffs Athat AreadFile Aexists. ATo
Akeep Aaway Afrom Aloading Athe Asame Amodule Aseveral Atimes, Ayou Aneed Aa
Astore A(cache) Aof Aalready Aloaded Amodules. AIf Acalled, Ait Aexamines Aif Athe
Ademanded Amodule Aloads Aand, Aif Anot, Ait Aloads Ait. AIt Arequires Areading
Athe Amodule’s Acode, Aenclosing Ait Awithin Aa Afunction Aand Acalling Ait. AThe
Alayout Aof Athe Aordinal Apackage Afrom Aearlier Ais Aa Afunction Aand Anot Aan
Aobject. AThe ACommonJS Amodules Acreate Aan Aempty Alayout Aobject Ausing
Athe Amodule Asystem Afor Ayou Ato Aexport, Arestore Athat Awith Avalues Aby
Abindings Aare Aaccessible Awithin Athe Amodule’s Ascope. AStrings Aset Ato
Arequire Ais Ainterpreted Ato Aa Afilename Aor Aweb Aaddress Avary Ain Aseparate
Asystems. AWhen Ait Astarts Awith A“. A/" Aor. A“/," Ait Ais Ausually Atranslated Aas
Arelative Ato Athe Aexisting Amodule’s Afilename. ASo A“. A/ Aformat-date" Awill
If Athe Aname Ais Anot Arelative, ANode.js Awill Asearch Afor Aa Apackage Athat Ais
Ainstalled Aby Athat Aname.
Now, Ainstead Aof Awriting Ayour Aown AINI Afile Aparser, Ayou Acan Autilize
Aone Afrom ANPM.
ECMAScript modulesA
Asubstantial Ascale. AThe Anotation Ais Aquite Aweird. AThe Athings Aadded Ato
Aexports Aare Anot Aaccessible Awithin Athe Alocal Ascope. AWithout Arunning
Athe Acode Aof Aa Amodule, Ait Awill Abe Atough Ato Adetermine Aits
AJavaScript Astandard Aestablishes Aits Aown, Aseparate Amodule Asystem. AIt Ais
Ainitial Aidea Aof Adependencies Aand Ainterfaces Astay Athe Asame, Abut Atheir
Adetails Aare Adifferent. AThe Anotation Aintegrates Ainto Athe Alanguage. AYou
AAn AES Amodule’s Alayout Adoes Anot Arepresent Aa Asingle Avalue Abut Aa Aset
Afunction. AIf Ayou Aimport Afrom Aa Adifferent Amodule, Athe Abinding Acomes
Awith Ait, Aand Anot Athe Avalue, Awhich Ameans Athe Avalue Aof Aan Aexporting
Amodule Acan Achange Athe Abinding Aany Atime, Aand Athe Aimporting Amodules
Awill Arecognize Aits Anew Avalue. AIf Aa Abinding Ais Anamed Adefault,
Arecognizes Ait Aas Athe Amodule’s Amajor Aexported Avalue. AIf Aa Amodule Alike
Aordinal Aimports Ain Athe Aexample, Aomitting Athe Abraces Aacross Athe
Abinding Aname, Ayou Awill Afind Aits Adefault Abinding. ATo Adevelop Aa
Adefault Aexport, Awrite Aexport Adefault Aahead Aof Aexpression, Aa Aclass Aor Aa
Afunction Adeclaration.
Awithin Afunctions Aor Ablocks, Aand Athe Adependent Anames Ashould Abe
Aquoted Astrings Aand Anot Aarbitrary Aexpressions. AA Alot Aof Aprojects Aare
Awritten Athrough AES Amodules Aand Athen Achanged Ato Asome Aother Aformat
Awhen Ait Ais Apublished. ADuring Aa Atransitional Aperiod, Ause Atwo Aseparate
Module Adesign
Program Astructuring Ais Aone Aof Athe Anice Afeatures Aof Aprogramming. AAny
Aunknown Afunctionality Acan Amodel Ain Adifferent Aways. AThe Agood
Aprogram Adesign Ais Aa Apersonalized Aone, Aand Athere Ais Aa Amatter Aof
Adesign, Ayou Aneed Ato Aread Aand Awork Aon Aseveral Aprograms Aand Atake
AINI Afile) Afunctions, Aand, Alike AJSON, Achanges Abetween Aplain Aobjects
Aand Astrings. AA Alot Aof Athe AINI-file Aparsing Amodules Aon ANPM Asupply Aa
Afunction Athat Areads Athat Atype Aof Afile Afrom Athe Ahard Adisk Aand Aparses
Ait. AConcentrated Amodules Athat Acalculate Avalues Aare Asuitable Ain Aa Alarger
Arange Aof Aprograms Athan Alarger Amodules Athat Aexecute Acomplex Aactions
Athat Ahave Aside Aeffects. AAn AINI Afile Areader Athat Awant Ato Aread Athe Afile
Afrom Adisk Ais Anot Auseful Ain Acases Awhereby Athe Afile’s Acontent Acomes
Anecessary Aand Auseful, Abut Aif Ayou Acan Ause Aa Afunction, Autilize Ait.
AVarious AINI Afile Areaders Aon ANPM Agives Aa Alayout Astyle Athat Aneeds
Ayou Ato Abuild Aan Aobject, Athen Aload Athe Afile Ainto Athe Aobject, Aand Ause
Aspecific Amethods Ato Aachieve Athe Adesired Aresults. AIt Ais Athe Aobject-
oriented Astyle Aand Atradition. AIn AJavaScript, Athere Ais Ano Aparticular Away
Ato Arepresent Aa Agraph. AThere Aare Avarious Apathfinding Apackages
Aavailable Aon ANPM, Abut Anone Autilizes Athe Agraph Aformat. AThey Aoften
Alet Agraph Aedges Acontain Aa Aweight, Aand Athat Ais Athe Adistance Aor Acost-
related Awith Ait. AFor Ainstance, Awe Ahave Athe Adijkstrajs Apackage. AA
Arecognized Away Ato Apathfinding, Arelated Ato Athe AfindRoute Afunction Ais
Awriter. AThe Ajs Asuffix Ais Aregularly Aattached Ato Apackage Anames Ato
Aspecify Athat Ayou Acan Awrite Ain AJavaScript. ASo, Aif Ayou Awant Ato Ause
Athat Apackage, Aensure Athat Athe Agraph Asaves Ain Aits Aexpected Aformat.
Aknow Aexactly Athe Adata Astructures Aothers Aare Ausing Aand Ayou Acan
Summary
In Athis Achapter, Awe Acovered Athe AModules. AModules Agive Astructure Ato
Amore Aextensive Aprograms Aby Adifferentiating Athe Acode Ainto Abits Awith
Awhich Ayou Acan Asee Afrom Aother Amodules, Aand Athe Adependencies Aare
Exercise
Write Aa AJavaScript Aprogram Ato Ashow Athe Acurrent Aday Aand Atime Ain Athe
Afollowing Aformat.
Athings Aoutside Aof Athe Aprocessor. AFor Ainstance, Athey Amay Ainteract
Athrough Aa Acomputer Anetwork Aor Ademand Afor Adata Afrom Athe Ahard
Adisk, Awhich Ais Aquite Aslower Athan Aobtaining Ait Afrom Amemory. AIn Apart,
Athe Aoperating Asystem Ahandles Athis Apart Aand Awill Achange Athe Aprocessor
Asynchronicity
In Athe Aasynchronous Aprogramming Amodel, Aeverything Aoccurs Aone Aat Aa
Atime. AIf Ayou Acall Aa Afunction Athat Aexecutes Aa Along Aduration Aaction,
Aresults Awill Areturn Awhen Athe Aoperation Acompletes Aonly. ANo Aother
Aaction Awould Abe Aable Ato Atake Aplace Aduring Aexecution. AAn
Aasynchronous Amodel Aenables Aseveral Athings Ato Aoccur Aat Athe Asame
Atime. AWhen Aan Aaction Abegins, Athe Aprogram Adoes Anot Astop Arunning.
AWhen Athe Atask Acompletes, Ait Anotifies Athe Aprogram Aand Acan Aaccess
Athe Aresult A(for Ainstance, Athe Ainformation Aread Afrom Adisk). AYou Acan
Acall Athe Arequests Aone Aafter Aanother. AIt Aconsists Aof Athe Adrawback Athat
Abegins Athe Asecond Arequest Aonly Awhen Athe Afirst Atask Acompletes. AThe
Asum Aof Aboth Aresponse Atimes Awill Abe Athe Atotal Atime Ataken. ATo Asolve
Acontrol. AA Athread Ais Aa Arunning Aprogram Athat Aits Aexecution Acould Abe
Ato Areturn, Aafter Athat, Athey Aresynchronize Ato Aunify Aboth Aresults. AIn Athe
Aasynchronous Amodel, Athe Anetwork Atime Ais Apart Aof Athe Atimeline Afor Aa
Aprogram Athat Acommences Athe Aaction Adoes Anot Astop Arunning, Aand Athen
Athe Aaction Aoccurs Awith Ait, Ait Ainforms Athe Aprogram Awhen Ait Acompletes.
Another Away Ato Adetermine Athe Asimilarities Ais Athat Awaiting Afor Atasks Ato
Acomplete Ais Aindirect Ain Athe Asynchronous Amodel, Awhile Ait Ais Adirectly
Callbacks
One Aprocedure Ato Aasynchronous Aprogramming Ais Ato Aenable Afunctions
Athat Aexecute Aa Aslow Aaction Ato Again Aan Aextra Aargument, Aand Athe
Aargument Ais Acalled Aa Acallback Afunction. AWhen Athe Aaction Abegins Aand
Aends, Ayou Ashould Acall Athe Aresult Awith Athe Acallback Afunction. AFor
Aand ANode.js, Awaits Aa Astated Anumber Aof Amilliseconds Aand Athen Acalls Aa
Aan Aideal Atype Aof Awork, Abut Acan Abe Aapplicable Awhen Ayou Aare Arunning
Aprograms Alike Aan Aanimation Aupdate Aor Achecking Aif Aa Afunction Ais
Atasks Ain Aa Arow Athrough Acallbacks Asignifies Athat Ayou Ahave Ato Acontinue
Asetting Anew Afunctions Ato Acontrol Athe Acontinuation Aof Athe Acalculations
A Alot Aof Acrow Anest Acomputers Aconsist Aof Aa Alongstanding Adata Astorage
Abulb, Awhere Adata Ais Astored Ainto Atwigs Aso Athat Ait Acan Abe Aaccessed
Aand Arestored Alater. AStoring, Aor Asearching Afor Aa Apiece Aof Adata Atakes
Alittle Atime, Aso Athe Alayout Ato Alongstanding Astorage Ais Aasynchronous
Aand Autilizes Acallback Afunctions. AStorage Abulbs Asave Abits Aof AJSON-
encodable Adata Aunder Anames. AA Acrow Acould Asave Adata Aabout Aplaces Ait
Ahas Ahidden Afood Aunder Athe Aname A"food Acaches," Athat Aholds Aan Aarray
Aof Anames Athat Apoint Aat Aother Apieces Aof Adata, Adefining Athe Acache. ATo
Asearch Afor Aa Afood Acache Ain Athe Astorage Abulbs Aof Athe ABig AOak Anest,
Anest Atransfers Aa Amessage Ato Athe Aother Anest, Awhich Ainstantly Areturns Aa
Ait Acontrols. AYour Acode Acan Adescribe Ahandlers Afor Acertain Arequest
Atypes, Aand Awhen Athe Arequest Acomes Ain, Athe Ahandler Ais Acalled Ato
Aprovide Aa Areply. AThe Alayout Aexported Aby Athe A“. A/crow-tech" Amodule
Asend Amethod Awhich Asends Aoff Aa Arequest. AIt Aawaits Athe Arequest Atype,
Athe Atarget Anest Aname, Aand Athe Arequested Acontent Aas Aits Afirst Athree
Aarguments, Aand Ait Aexpects Aa Afunction Ato Acall Awhen Aa Areply Acomes Ain
Aneed Ato Arun Aand, Aon Aall Anests, Acan Areceive Athis Atype Aof Amessages.
ALet Aus Astate Athat Aa Acrow Aflies Aover Aand Ainstalls Aour Ahandler Acode Aon
Aall Athe Anests.
Ato Aa Astated Anest. AOur Aimplementation Acalls Aconsole.log Aso Athat Awe
Acan Acheck Aif Athe Arequest Ahas Aarrived. ANests Aconsist Aof Aa Aname
Aproperty Athat Aholds Aits Aname. AThe Afourth Aargument Astated Ato Athe
Ahandler, Adone, Ais Aa Acallback Afunction Athat Acalls Awhen Ait Ahas
Acompleted Athe Atask. AIf Ayou Ause Athe Ahandler’s Areturn Avalue Aas Athe
Avalue Aof Athe Aresponse, Athat Ameans Athat Aa Arequest Ahandler Acan’t
Acallback Aready Ato Abe Acalled Aupon Awhen Ait Afinishes. AIn Aa Away,
Acallback Ato Adeliver Aits Aresult. ACalling Aa Acallback Ais Aerror-prone Aand
Promises
Working Awith Aabstract Amethods Ais Asimpler Awhen Avalues Arepresent Athe
Amethods. AIn Athe Acase Aof Aasynchronous Aactions, Ainstead Aof Amaking
Apreparations Afor Aa Afunction Ato Abe Acalled Aat Aa Acertain Apoint Ain Athe
Afuture, Asend Aback Aan Aobject Athat Astands Afor Athe Afuture Aevent. AA
Apromise Ais Aan Aasynchronous Aaction Athat Acan Afinish Aat Asome Apoint
Aand Aprovide Avalue. AIt Acan Anotify Aanyone Ainterested Awhen Aits Avalue Ais
Aavailable. AThe Asimplest Away Ato Abuild Aa Apromise Ais Aby Acalling
Aencloses Ain Aa Apromise. AIf Ait Ais Aa Apromise Aalready, Ait Areturns.
AOtherwise, Aa Anew Apromise Awill Abe Acreated Ainstantly Awith Ayour Avalue
Aas Aa Aresult.
Aprovides Aa Avalue. AYou Acan Aattach Aseveral Acallbacks Ato Aan Aindividual
Apromise, Aand Athey Awill Acall Athe Acallbacks, Aeven Aif Athey Awere
Aattached Awhen Athe Apromise Ahas Abeen Acompleted. AThe Athen Amethod
Aalso Asend Aback Aanother Apromise Athat Asolves Athe Avalue Athat Athe
Ahandler Afunction Areturns. AIt Ais Aimportant Ato Asee Apromises Aas Aa Adevice
Ato Aturn Avalues Ainto Aan Aasynchronous Areality. AA Apromised Avalue Ais Aa
Avalue Athat Acould Abe Athere Aor Acould Adisplay Aat Aa Acertain Apoint Ain Athe
Aconstructor. AIt Acontains Aan Aodd Alayout, Aand Athe Aconstructor Aexpects Aa
Afunction Aas Aan Aargument, Awhich Ait Acalls Ainstantly, Asending Ait Aa
Afunction Athat Acan Abe Aused Ato Asolve Athe Apromise. AIt Ais Ahow Ato Abuild
Areveal Acorrectly Ato Athe Acallbacks. AA Agenerally Aused Amethod Ais Athat
Athe Afirst Aargument Ato Athe Acallback Ais Aused Ato Aspecify Athe Afailed
Aaction, Aand Athe Asecond Ahas Athe Avalue Aprovided Aby Athe Aaction Awhen
Ait Acompletes. ASuch Acallback Afunctions Aalways Acheck Aif Athey Aobtain
Aexceptions Aoutput Aby Afunctions Athey Acall, Aare Agiven Ato Athe Acorrect
Adeclined Awhen Ait Afails. ACall AReduce Ahandlers Ait Aonly Awhen Athe Atask
Athis Aautomatically Atriggers Athe Apromise Aprovided Aby Aits Arecent Acall
Adeclines. AWhen Aan Aelement Ain Aa Achain Aof Aasynchronous Aactions Afails,
Athe Aoutput Aof Athe Awhole Achain Adeclines Aand Ano Asuccess Ahandlers Acall
Avalue, Arejecting Aone Aalso Aprovide Aone, Aoften Acalled Athe Arejection
Areason. AIf Aan Aexception Ain Aa Ahandler Afunction Acreates Athe Arejection,
Ause Athe Aexception Avalue Aas Athe Areason. AComparably, Awhen Aa Ahandler
Asends Aback Aa Arejected Apromise, Athat Arejection Agoes Ainto Athe Anext
If Aa Acatch Ahandler Aoutput Aan Aerror, Athe Anew Apromise Ais Aequally
Adeclined. AIn Ashorthand, Athen Aalso Atake Aa Adecline Ahandler Aas Aa Asecond
Aargument, Aso Ayou Ashould Ainstall Athe Atwo Atypes Aof Ahandlers Ain Aan
Awhich Ait Acan Ause Ato Adecline Athe Anew Apromise. AThe Achains Aof
Apromise Avalues Adeveloped Aby Acalls Ato Athen Aand Acatch Arecognized Aas
Athose Achains Abuilt Aby Aregistering Ahandlers, Aevery Alink Aconsists Aof
Aeither Aboth Aor Aa Asuccess Ahandler Aor Aa Arejection Ahandler Arelated Ato Ait.
AHandlers Athat Ado Anot Amatch Athe Atype Aof Aoutput Adeclined. ABut Athose
Athat Amatch Agets Acalled, Aand Atheir Aoutput Adecides Awhat Atype Aof Avalue
Arejection Awhen Ait Aoutput Aan Aexception, Aand Athe Aresult Aof Aa Apromise
Afor Aa Asignal Ato Abe Atransferred Abut Agot Adeclined. AOften, Atransmission
Alight Asignals, Aand Aresending Athe Arequest Acould Amake Ait Asucceed. ASo,
Alet’s Acreate Aour Arequest Afunction, Aand Aconsequently Aretry Athe Asending
Aof Athe Arequest Amore Atime Abefore Ait Astops. AAnd, Asince Awe Ahave
Aestablished Athat Apromises Aare Agood, Awe’ll Aalso Aensure Aour Arequest
Afunction Asends Aback Aa Apromise. AWhen Ait Acomes Ato Awhat Athey Acan
AEven Awhen Aa Arequest Aand Aits Areply Aconvey Asuccessfully, Athe Areply
Acould Aspecify Afailure, Afor Ainstance, Aif Athe Arequest Atries Ato Autilize Aa
Arequest Atype Athat Ahas Anot Abeen Adescribed Aor Athe Ahandler Aoutput Aan
Acallbacks Ais Athe Areason Afor Afailure Aif Aany, Aand Athe Asecond Ais Athe
ADefinite Aresult. AThey Aare Ainterpreted Ato Apromise Aresolution Aand
Arejection Aby Aour Awrapper.
Apromise, Aand Aany Aother Acalls, Aare Adisregarded. ATo Acreate Aan
Aefficient Aloop Adoes Anot Aallow Aus Ato Astop Aand Await Afor Aan
Ato Atransfer Aa Arequest. AIt Aalso Aspecifies Aa Atimeout Athat, Aif Athere Ais
Aor, Aif Ait Ais Athe Afourth Atry, Adeclines Athe Apromise Awith Aan Ainstance Aof
Awhen Athere Ais Ano Aresponse Aafter Aa Asecond Ais Acertainly Aarbitrary Aif
Athe Arequest Ashows Aup, Abut Athe Ahandler Atakes Alonger Afor Arequests Ato
Atransfer Aseveral Atimes. ACreate Ayour Ahandlers Awith Athat Aproblem Ain
Amind, Aand Acoupled Amessages Aare Anot Aharmful. ATo Asegregate Aourselves
Aenables Athe Ahandler Afunction Ato Asend Aback Aa Apromise Aor Asimple
Collections of promises
A A
Every Anest Acomputer Astores Aan Aarray Aof Aother Anests Ainside
Atransmission Adistance Ain Aits Aneighbor’s Aproperty. ATo Aexamine Awhich
Ais Areachable, Awrite Aa Afunction Athat Atries Ato Atransfer Aa A"ping" Arequest
A(request Athat Ademands Aa Aresponse) Ato Aall Aof Athem Aand Acheck Afor
Awhich Aones Acome Aback. AIf Ayou're Aworking Awith Aa Agroup Aof Apromises
Arunning Aat Aonce, Athe APromise.all Afunction Ais Avery Aimportant. AIt Asends
Aback Aa Apromise Athat Aholds Aon Afor Aall Aof Athe Apromises Ain Athe Aarray
Ato Areconcile Aand Athen Asettle Ato Aan Aarray Aof Athe Avalues Athat Athese
Apromises Aprovided. AIf Aany Apromise Ais Adeclined, Athe Aresult Aof
Athe Aentire Anetwork, Aa Asolution Ais Ato Acreate Aa Atype Aof Arequest Athat Ais
Asent Aautomatically Ato Aneighbors. AThese Aneighbors Athen Asend Ait Ato
Atheir Aneighbors Auntil Athe Aentire Anetwork Ahas Acollected Athe Amessage.
ATo Adescribe Athis Aarray, Auses Athe Aeverywhere Afunction Athat Aruns Acode
Aon Aeach Anest Ato Aattach Aa Aproperty Ato Athe Anest’s Astate Aobject.
Message Arouting
If Aa Aset Anode Awants Ato Acommunicate Awith Aa Asingle Aother Anode. AThe
Abest Aapproach Ais Ato Abuild Aa Apath Afor Amessages Ato Ajump Afrom Anode
Ato Anode Auntil Athey Aget Ato Atheir Adestination. AYou Aneed Ato Aunderstand
Athe Ainterface Aof Athe Anetwork. ATo Asend Aa Arequest Ato Aa Anest Aat Aa
Adistance, Ait Ais Aimportant Ato Aknow Awhich Aneighboring Anest Acan Aget
Athe Ajob Adone. AEvery Anest Aknows Aabout Aits Adirect Aneighbors Aonly, Ait
Aunderstand Ahow Ato Aspread Athe Ainformation Aabout Athese Aconnections Ato
Aevery Anest Ain Aa Away Athat Aenables Ait Ato Atransform Awith Atime, Awhen
Anew Anests Aare Acreated. ANow Ayou Acan Ause Aflooding, Abut Ainstead Aof
Achecking Awhether Aa Amessage Ahas Abeen Acollected, Anow Acheck Aif Athe
Anew Aset Aof Aneighbors Afor Aa Agiven Anest Ais Asimilar Ato Athe Acurrent Aset.
Agive Athem Aa Amap Aof Athe Aexisting Anetwork Agraph. AThe AfindRoute
Afunction Afinds Aa Apath Ato Areach Aa Aset Anode Awithin Athe Anetwork. ABut
Ainstead Aof Asending Aback Athe Awhole Aroute, Ait Ajust Abrings Aback Athe
Anext Astep. AThat Anext Anest Awill Ause Aits Aexisting Ainformation Aabout Athe
Aconveyed Aas Ausual. AIf Anot, Ait Ais Awrapped Ain Aan Aobject Aand Atransferred
Ato Aa Aneighbor Acloser Ato Athe Atarget, Athrough Athe A"route" Arequest Atype,
Async Afunctions
To Asave Asignificant Ainformation, Acrows Ause Athe Aduplication Amethod,
Athey Aduplicate Ait Aacross Anests. AThat Away, Awhen Aa Abird Adismantles Aa
Anest, Athe Ainformation Awould Anot Abe Alost. ATo Arecover Aa Aparticular
Apiece Aof Ainformation Athat Ahas Ano Astorage Abulb, Aa Anest Acomputer
Awould Atalk Ato Arandom Aother Anests Ain Athe Anetwork Auntil Athe Aone Athat
Aarray. AAn Aiterator Acan Abe Atransformed Ato Aan Aarray Ausing Athe
Afunction Athat Acompletely Abring Aback Aa Apromise Aand Athat Acan Await Afor
Athis:
Abefore Atheir Aname. AWhen Athe Afunction Aor Amethod Ais Acalled, Ait Abrings
Aback Aa Apromise. AWhen Athe Abody Areturns Asomething, Athat Apromise Ais
Asettled. AIf Ait Aoutputs Aan Aexception, Athe Apromise Ais Adeclined. AWithin Aan
Aasync Afunction, Athe Aword Aawait Ais Aset Ain Afront Aof Aan Aexpression Ato
Aawait Aa Apromise Ato Aget Asettled Aand Aonly Athen Aproceeds Ato Aexecute Athe
Afunction. ASuch Afunctions Acannot Arun Afrom Astart Ato Afinish Aat Aonce, Abut
Ait Acan Abe Afrozen Aat Apoints Aand Acan Arestart Alater. AFor Anon-trivial
Aasynchronous Acode, Athis Anotation Ais Aeasier Athan Adirectly Athrough
Apromises.
Generators
Async Afunctions Ado Anot Ahave Ato Abe Apaused Aand Athen Aresumed Aagain.
AJavaScript Aconsists Aof Aa Afeature Acalled Agenerator Afunctions. AThese Aare
Aidentical Abut Ado Anot Acontain Athe Apromises. AWhen Ayou Acall Aa Afunction
Awith Afunction* A(put Aan Aasterisk Aafter Athe Aword Afunction), Ait Achanges
Ato Aa Agenerator. AWhen Ayou Acall Aa Agenerator, Ait Asends Aback Aan Aiterator.
function* Apowers(n) A{
for A(let Acurrent A= An;; Acurrent A*= An) A{
yield Acurrently;
}
}
for A(let Apower Aof Apowers(3)) A{
if A(power A> A50) Abreak;
console.log(power);
}
// A→ A3
// A→ A9
// A→ A27
Normally, Aif Ayou Acall Apowers, Athe Afunction Ais Afrozen Aat Athe Abeginning.
AEach Atime Ayou Acall Anext Aon Athe Aiterator, Athe Afunction Awill Arun Auntil
Ait Ahits Aa Ayield Aexpression, Awhich Awill Apause Ait Aand Amake Athe Ayielded
Avalue Athe Anext Avalue Aprovided Aby Athe Aiterator. AWhen Athe Afunction
Areturns, Athe Aiterator Acompletes. AWriting Aiterators Ais Asimple Awhen Ayou
Autilize Athe Agenerator Afunctions. AWrite Athe Aiterator Afor Athe AGroup Aclass
Group.prototype[Symbol.iterator] A= Afunction*() A{
for A(let Ai A= A0; Ai A< Athis.members.length; AI++) A{
yield Athis.members[i];
}
};
There’s Ano Aneed Ato Abuild Aan Aobject Ato Ahold Athe Aiteration Astate, Aand
Agenerators Astore Atheir Alocal Astate Aimmediately Aand Athey Ayield Aevery
Atime. AThis Ayield Aexpression Acan Ahappen Aonly Adirectly Awithin Athe
Agenerator Afunction Aand Anot Ain Aan Ainner Afunction Athat Ayou Aset Awithin
Ait. AThe Agenerator Asaves Awhen Ayielding. AThis Ais Aits Alocal Aenvironment
Aand Athe Aposition Awhere Ait Ayields. AAn Aasync Afunction Ais Aa Aunique Akind
Aof Agenerator. AIt Acreates Aa Apromise Awhen Ait’s Acalled, Awhich Aresolves
Awhen Ait Acompletes Aand Adeclines Awhen Ait Aoutputs Aan Aexception.
AAnytime Ait Ayields Aa Apromise, Athe Aresult Aof Athat Apromise Awill Abe Athe
Asynchronous Aprograms Aperform Aone Aby Aone. AEvery Apiece Amay Abegin
Aa Afew Aactions Aand Aarrange Acode Ato Aexecute Awhen Athe Aaction
Acompletes Aor Afails. ABetween Athese Apieces, Athe Aprogram Adoes Anothing,
Aawaiting Athe Anext Aaction. ASo Ayou Acannot Acall Acallbacks Aby Athe Acode
Athat Aarranged Athem. AIf Ayou Acall AsetTimeout Afrom Ainside Aa Afunction,
Athat Afunction Awill Ahave Abeen Asent Aback Abefore Athe Acallback Afunction
Acalled Astack. ASince Aeach Acallback Abegins Awith Aan Aempty Astack, Ayour
Acatch Ahandlers Awill Anot Abe Aon Athe Astack Awhen Aan Aexception Aruns.
try A{
setTimeout(() A=> A{
throw Anew AError("Woosh");
}, A20);
} Acatch A(_) A{
// AThis Awill Anot Arun
console.log("Caught!");
}
No Amatter Ahow Aevents Alike Atimeouts Aor Aincoming Arequests Aoccur, Aa
AJavaScript Aenvironment Aruns Aone Aprogram Aat Aonce. AWhen Athere’s
Anothing Ato Ado, Athe Aloop Astops. ABut Aas Aevents Acome Ain, Athey Aare
Aattached Ato Aa Aqueue, Aand Atheir Acode Aperforms Aone Aafter Aother.
ABecause Atwo Athings Ado Anot Arun Aat Aonce, Aslow-running Acode Amay Ahold
Aup Athe Ahandling Aof Aother Aevents. AThe Abelow Aexample Asets Aa Atimeout
Abut Aalso Alingers Auntil Aafter Athe Atimeout’s Afixed Apoint Aof Atime, Amaking
Promise.resolve("Done").then(console.log);
console.log("Me Afirst!");
// A→ AMe Afirst!
// A→ ADone
Asynchronous Abugs
When Ayou Arun Ayour Aprogram Asynchronously, Athere Ais Ano Agiven
Aoccurring Atransformation Abesides Athe Aones Athat Aprogram Athemselves.
ABut Awhen Ayou Arun Aan Aasynchronous Aprogram, Athis Ais Aanother
Ascenario. AThey Acould Acontain Agaps Aduring Aexecution Athat Aother Acode
Acan Autilize. ABelow Ais Aan Aillustration. AOne Aof Aour Acrow’s Afavorites Ais
Ato Aenumerate Athe Anumber Aof Achicks Athat Ahatch Aduring Athe Avillage
Aeach Ayear. ANests Akeep Athis Acount Awithin Atheir Astorage Abulbs. AThe
Abelow Acode Atries Ato Atally Athe Acounts Afrom Aevery Anest Afor Aa Aparticular
Ayear:
Athe Aasync Aarrow Afunction Aacross Athe Aset Aof Anests, Abuilding Aan Aarray
Aof Apromises, Aand Aalso Autilizing APromise.all Ato Await Abefore Asending
Aback Athe Alist Athey Acreated. ABut Ait's Abroken. AIt’ll Asend Aback Aa Asingle
Aline Aof Aoutput, Alisting Athe Aslowest Ato Arespond Anests. ADo Ayou Aknow
Awhy?
The Aproblem Ais Ain Athe A+= Aoperator Athat Atakes Athe Apresent Avalue Aof
Athe Alist Awhen Athe Astatement Abegins Aexecuting Aand Awhen Athe Await
Aends, Asets Athe Alist Abinding Ato Abe Athe Avalue Atogether Awith Athe Aadded
Astring. ABut Abetween Athe Abeginning Aand Aend Aof Athe Astatement, Aan
Aasynchronous Agap Ais Apresent. AThe Amap Aexpression Ais Athe Afirst Ato Arun
Abefore Ayou Acan Aadd Aanything Ato Athe Alist, Aso Aevery A+= Aoperator
Abegins Afrom Aan Aempty Astring Aand Afinishes Awhen Aits Aretrieval Astorage
Acompletes. AAs Ausual, Acalculating Anew Avalues Ais Aan Aerror-free Aprocess
Summary
We Alearned Aeverything Aabout AAsynchronous Aprogramming Ain Athis
Achapter. AAsynchronous Aprogramming Aenables Athe Aexpression Awaiting
Afor Aaction Athat Aruns Afor Aa Along Atime Awithout Agiving Athe Aprogram
Ato Acall Awhen Aactions Aend. AWe Atouched Aon Athe Aevent Aloop Aas Awell.
AAn Aevent Aloop Acreates Aa Aplan Afor Asuch Acallbacks Ato Abe Acalled Awhen
Exercise
Write Aa AJavaScript Aprogram Ato Atransform Aan Aasynchronous Afunction Ato
Areturn Aa Apromise.
Solution
Chapter 13: Parsing
Developing Aone's Aprogramming Alanguage Ais Arelatively Aeasy Aand Avery
Aexciting. AThis Achapter Awill Ateach Ayou Ahow Ato Abuild Ayour Alanguage.
Alanguage Ais Ayet Astrong Aenough Ato Aexpress Aany Athinkable Acomputation.
Parsing
The Anoticeable Apart Aof Aa Aprogramming Alanguage Ais Athe Anotation Aor
Asyntax. AA Aparser Ais Aa Aprogram Athat Areads Athe Atext Aand Aprovides Aa
Adata Alayout Athat Areflects Athe Alayout Aof Athe Aprogram Awithin Athat Atext.
AIf Athe Atext Arefuses Ato Acreate Aa Avalid Aprogram, Athe Aparser Awould
Aindicate Aan Aerror. AOur Alanguage Awould Acontain Aprecise Aand Auniform
Asyntax. AEverything Aincluded Ain AEgg Ais Aan Aexpression. AAn Aexpression
Acould Abe Athe Abinding Aname, Anumber, Astring, Aor Aapplication. AUtilize
AApplications Afor Afunction Acalls Aand Aas Awell Aas Aconstructs Alike Aif Aor
Awhile.
To Akeep Athe Asimple Aparser, Astrings Awithin Athe AEgg Ado Anot Asupport
Abackslash Aescapes. AA Astring Ais Aa Asuccession Aof Acharacters Athat Aare Anot
Aany Acharacter Athat Aisn't Awhitespace Aand Athat Ado Anot Acontain Aa Aunique
Ameaning Ain Athe Asyntax. AWrite Aapplications Ain AJavaScript, Aby Aplacing
Aparentheses Aafter Aan Aexpression Aand Aputting Aany Anumber Aof Aarguments
do(define(x, A10),
if(>(x, A5),
print("large"),
print("small")))
The Aconsistency Aof Athe AEgg Alanguage Ameans Athat Aoperators Ain
AJavaScript A(like Athe A>) Aare Acalled Abindings Ain Athis Alanguage, Autilized
Asimilarly Alike Aother Afunctions. AAnd Athe Asyntax Acontains Azero Ablock
Aconcept, Aso Aa Aconstruct Ais Aneeded Ato Arepresent Arunning Aseveral Athings
Ain Asuccession. AThe Adata Alayout Athe Aparser Autilizes Ato Adefine Aa
Aprogram Acontaining Aexpression Aobjects. AEach Aone Acontains Aa Atype Aof
Aproperty Asignifying Athe Akind Aof Aexpression Ait Ais Aand Aseveral Aother
Aproperties Ato Adetermine Aits Acontent. AExpressions Aof Atype A"value" Amean
Astrings Aor Anumbers. ATheir Avalue Aproperty Aconsists Aof Athe Astring Aor
Anumber Avalue Athat Athey Astand Afor. AUse Aexpressions Aof Atype A"word"
Aproperty Athat Aholds Athe Aidentifier’s Atitle Aas Aa Astring. AAnd Athen Athe
Aproperty Athat Acites Athe Aapplied Aexpression, Atogether Awith Aan Aargs
The A>(x, A5) Apart Aof Athe Anew Aprogram Awould Abe Alike Athis:
{
type: A"apply",
operator: A{type: A"word", Aname: A">"},
args: A[
{type: A"word", Aname: A"x"},
{type: A"value", Avalue: A5}
]
}
We Adescribe Aa Afunction AparseExpression, Awhich Arecognize Aa Astring Aas
Ainput Aand Asend Aback Aan Aobject Athat Aconsists Aof Athe Adata Alayout Afor
Athe Aexpression Aat Athe Abeginning Aof Athe Astring, Atogether Awith Athe
Astring Apart Aleft Aafter Aparsing Athis Aexpression. AWhen Ayou Aare Aparsing
Aargument Aexpression Awith Athe Aremaining Atext. AThis Atext Acould Aconsist
Aof Amore Aarguments Aor Athe Aclosing Aparenthesis Athat Aends Athe Aargument
Alist.
Awill Aaccess Athe Aexpression Athat Athe Atree Astands Afor Aand Asend Aback Athe
Athe Ascope Aand, Aif Ayes, Aget Athe Avalue Aof Athe Abinding. AIf Ait Acontains Aa
Aunique Aform Athat Adoes Anot Aaccess Aanything, Aand Ayet Atransfers Athe
Aargument Aexpressions Awith Ascope, Ato Athe Afunction Athat Ainfluences Athe
Aform. AIf Athe Acall Ais Atypical, Aaccess Athe Aoperator, Adetermine Aif Ait Ais Aa
Afunction, Aand Acall Ait Awith Athe Aaccessible Aarguments. AUtilize Athe
Acommon AJavaScript Afunction Avalues Ato Astand Afor Athe Avalue Aof AEgg’s
Afunction. AThe Arecursive Alayout Aof Aevaluating Ais Aa Asimilar Alayout Aof
Athe Aparser, Aand Athe Atwo Amirror Athe Alayout Aof Athe Alanguage. AYou Acan
Acombine Athe Aparser Ausing Athe Aevaluator Aand Aevaluate Awhen Aparsing,
Abut Aseparating Athem Agives Aa Aclearer Aof Athe Aprogram. AIt Ais Aall Ayou
Special forms
A
The AspecialForms Aobject Adescribes Aunique Asyntax Ain AEgg. AIt Arelates
Awords Awith Afunctions Athat Aaccess Asuch Aforms. AIt Ais Apresently Aempty.
Ait Aaccesses Athe Athird. AThe Aif Aform Alooks Amore Alike AJavaScript’s
Aternary?: Aoperator Athan AJavaScript’s Aif. AIt Ais Aan Aexpression Aand Anot Aa
Astatement, Aand Aprovides Aa Avalue, Awhich Aare Athe Aresults Aof Athe Asecond
Aor Athird Aargument. AEgg Aare Aalso Adifferent Afrom AJavaScript Ain Ahandling
Athe Acondition Avalue Ato Ait. AIt Adoes Anot Arelate Awith Athings Alike Azero Aor
Aempty Astrings Aas Afalse, Aonly Athe Aexact Avalue Afalse. AAll Aarguments Ato
Aexpression Aproviding Athe Avalue Ato Aset Ato Athat Aword Aas Aits Asecond
Aargument. ASince Adefine Ais Aan Aexpression, Ait Amust Asend Aback Aa Avalue.
AWe Awill Areturn Athe Avalue Athat Awas Aset A(like AJavaScript’s A= Aoperator).
Aaccordance Ato Athe Avalues Athose Abounded Abindings. ABelow Ais Aan
Aillustration Aof Aan Aobject Ato Astand Afor Aglobal Ascope. ATo Aenable Athe
Autilization Aof Athe Aearlier Adescribed Aif Aconstruct, Athe ABoolean Avalues
Amust Abe Aaccessible. ATherefore, Athere Aare Atwo ABoolean Avalues Aonly, Aall
Ayou Aneed Ato Ado Ais Ato Abind Atwo Anames Ato Athe Avalues Atrue Aand Afalse
Aoperator Afunctions Awithin Aa Aloop, Aand Anot Astating Athem Aone Aby Aone.
for A(let Aop Aof A["+", A"-", A"*", A"/", A"==", A"<", A">"]) A{
topScope[op] A= AFunction("a, Ab", A`return Aa A${op} Ab;`);
}
we Awill Aenclose Aconsole.log Ain Aa Afunction Ato Aoutput Athe Avalues
and Acall Ait Aprint.
topScope.print A= Avalue A=> A{
console.log(value);
return Avalue;
};
That Aprovides Aadequate Aelementary Atools Ato Acode Aeasy Aprogram,
Aanalyze Aand Arun Ait Ain Aa Anew Ascope:
function Arun(program) A{
return Aevaluate(parse(program), AObject.create(topScope));
}
The Achains Aof Athe Aobject Aprototype Awill Astand Afor Anested Ascopes Ato
Aenable Aadd Abindings Awithout Aaltering Athe Ahigh-level Ascope.
run(`
do(define(total, A0),
define(count, A1),
while(<(count, A11),
do(define(total, A+(total, Acount)),
define(count, A+(count, A1)))),
print(total))
`);
// A→ A55
This Aconcept Aexplains Athe Atotal Anumbers Afrom A1 Ato A10, Aas Aindicated
Ain Aegg.
Functions
Programming Alanguage Athat Ahas Ano Afunctions Ais Aa Areally Abad Aone,
Aalthough Ait Ais Aeasy Ato Ainsert Aa Afunction Athat Auses Aits Alast Aargument Aas
Athe Afunction’s Abody. AIt Aalso Autilizes Aevery Aargument Abefore Athat Aas
Abindings Ato Ait. AIt Athen Aaccesses Athe Afunction Abody Ain Athis Ascope Aand
run(`
do(define(plusOne, Afun(a, A+(a, A1))),
print(plusOne(10)))
`);
// A→ A11
run(`
do(define(pow, Afun(base, Aexp,
if(==(exp, A0),
1,
*(base, Apow(base, A-(exp, A1)))))),
print(pow(2, A10)))
`);
// A→ A1024
Summary
We Acovered Athe Aparsing, Afunctions, Aspecial Aforms, Aand Athe Aevaluator Ain
Athis Achapter. ANotation Aor Asyntax Ais Aa Avery Aimportant Aaspect Aof
Ashows Athe Aprogram Alayout Ain Athe Atext. AIf Athe Atext Acreates Aan Ainvalid
Exercise
Write Aa AJavaScript Afunction Ato Acalculate Athe Afactors Aof Aa Apositive
Ainteger.
Solution
Conclusion
This Abook Aconsists Aof Aimportant Ainformation Aconnected Ato Athe
AJavaScript Aprogramming Alanguage. AThe Ainformation Ain Athis Abook Awill
Ahelp Ayou Ato Aunderstand Aand Apractice Athe Aseveral AJavaScript Aoperators,
Ainvent Atypes, Aand Aso Aon. AAnd Awhile Aedging Atoward Athe Aend Aof Athe
Acourse, Ayou Awill Ahave Aan Aexcellent Aunderstanding Aof Athe AJavaScript
Alanguage Aas Ayou Awill Ahave Alearned AJavaScript Aand Aforms, Ayou Awill
Aunderstand Athe Abasics Aof AjQuery, Aframes Aand Adebugging Ascripts. AThe
Abest Aadvice Ato Agive Aa Aprogrammer Ais Ato Akeep Apracticing Abecause Athat
Ais Athe Aonly Away Ayou Acan Amaster Awhat Ayou Ahave Alearned. A