SlideShare a Scribd company logo
1 of 20
JavaScripts internals #1 
Martin Pernica | @martindeveloper
Primitive data types in JS 
How many are there? 
JavaScript contains only 3 primitive types – string, number, boolean 
These types arent objects. JS runtime wraps these types using objects wrappers 
String(), Number() a Boolean().
Primitive data types in JS 
Every primitive type is immutable. 
Which means, if you work with variable, everytime will be created copy and original 
stay untouched – „copy by value“ vs „ copy by reference“.
Data types in JS 
•null 
•undefined 
•boolean 
•number 
•string 
•object 
•symbol (ES6) 
Null is special, yeah thats mean – buggy. 
typeof null === "object"; // true
Null magic 
This null bug is in JS for 2 decades and maybe never will be fixed. 
Because many code using this bug behavior. 
How can I test value if it is null properly? 
var a = null; 
(!a && typeof a === "object"); // true 
This works because null is “falsy like”.
Undefined 
Undefined is value for variables which are defined (in valid scope) but doesn’t have 
value yet. 
var a; 
typeof a === “undefined"// true 
But I need check if is variable defined at all and can be accessed from current scope, 
what now? 
If you use variable which is not defined or is not accessible from current scope, 
runtime will fire error ReferenceError.
Undefined 
If you use typeof function on non exist variable, it will return “undefined”. 
var a; 
typeof a; // undefined 
typeof b; // undefined 
But it will not trigger the error!
Note about Objects 
Objects are not primtive types. They are composite type. 
Composite type holds primitive types and others composite types. 
Objects are simple look-tables and compiler break them into primitive types.
Scopes 
Like in others programming language use variables for data storing and scopes for 
determining when and where can be variable accessed. 
JavaScript is dynamic language and scoping process can be little bit different than in 
other languages.
Scopes 
JavaScript (simplified) have: 
• Engine 
o Engine must ensure program compilation and execution 
• Compiler 
o Compiler will parse code and generate “native” code for Engine 
• Scope 
o Big table which collects declared variables and have strict rules for accessing them.
Scopes 
var a = 2; 
1. Compiler will break it down into tokens and parse them into token tree. 
2. After lexing compiler will do code generation (for engine). 
3. When compiler reach part “var a”, it asks scope if variable is exist and is accessible. 
If true, compiler will skip declaration. If false, compiler will ask scope to declare 
new variable called “a” in current scope. 
4. After that compiler will produce code for engine, to handle part “a = 2”. Engine 
runs will first ask Scope if “a” is accessible from current scope collection. If true, 
engine will use this “a” variable. If false, engine will looks into nested scopes. 
5. If Engine cant find variable “a” in nested scopes, it will raise error.
Scopes 
When engine needs to look-up for variable “a”, it will use Left hand Side look-up. 
There is also Right hand Side look-up. 
LHS is trying to find variable container (simplified – area of memory). 
RHS is trying to find variable value (simplified – content of memory area). 
LHS or RHS is not only used when engine working with variable, it is used when 
working with functions, objects and other things also.
Nested scopes 
Sometimes code is nested in more than one scope. 
For example – one block is nested inside function and 
function is nested in object and etc. 
Engine always trying to look-up every outer scope until 
found or until when reach last „global“ scope.
Nested scopes - errors 
If RHS look-up fails find variable anywhere, the Engine will throw ReferenceError. 
But if LHS look-up fails (at global scope), then Scope will create variable in global scope 
with that name and it will return it to Engine. 
*This “auto creating variable” behavior is not correct and it will fails when program will 
run in Strict Mode and it will throw ReferenceError too.
Scopes – last words 
Sometimes you can “cheat” scoping behavior using 
eval() or with keyword (deprecated). Do not use them! 
It will cheat your program, you can make more 
mistakes and it will be much slower (because you are 
trying to create scope in runtime and without code 
optimalizations)! 
Scope look-up stops once it finds the first match. You 
can have same variables in different scopes, but the 
„last win“. This behavior is called shadowing. 
Every function have own scope.
Anonymous functions 
Many JS libraries use anonymous function idiomatic style of code. 
Anonymous function are easy to write but have some problems: 
• They don’t have a name – bad debugging and you cant call them recursive 
But you can write inline function expression, it is anonymous function with name. 
You don’t have declare them outside, but when you write anonymous function, 
prepend name and that’s it!
Fun fact 
(new Date()).getMonth(); 
Result? 
8
Questions?
Next? 
1. Scope closures 
2. Dynamic scope 
3. Hoisting 
4. Garbage Collector 
5. Objects (this) 
6. Prototypes 
7. ES6 features – let, const …
Thank you for your attention! 
And thank you to authors of great book series „You Dont Know JS“! 
http://shop.oreilly.com/product/0636920026327.do 
http://shop.oreilly.com/product/0636920033738.do 
https://github.com/getify/You-Dont-Know-JS 
Martin Pernica | @martindeveloper

More Related Content

What's hot

Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design PatternsNLJUG
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_iNico Ludwig
 
10. symbols | ES6 | JavaScript | TypeScript
10. symbols | ES6 | JavaScript | TypeScript10. symbols | ES6 | JavaScript | TypeScript
10. symbols | ES6 | JavaScript | TypeScriptpcnmtutorials
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part維佋 唐
 
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT ginriki
 
Introduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryIntroduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryKnoldus Inc.
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for IntermediatesAnkit Agrawal
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...DevClub_lv
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPISlicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPIlestrrat
 
Getting started with scala cats
Getting started with scala catsGetting started with scala cats
Getting started with scala catsKnoldus Inc.
 
Lecture 3 - ES6 Script Advanced for React-Native
Lecture 3 - ES6 Script Advanced for React-NativeLecture 3 - ES6 Script Advanced for React-Native
Lecture 3 - ES6 Script Advanced for React-NativeKobkrit Viriyayudhakorn
 
Javascript omg!
Javascript omg!Javascript omg!
Javascript omg!bwullems
 
Mistakes made with string object in java
Mistakes made with string object in javaMistakes made with string object in java
Mistakes made with string object in javaTutors On Net
 

What's hot (20)

Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_i
 
10. symbols | ES6 | JavaScript | TypeScript
10. symbols | ES6 | JavaScript | TypeScript10. symbols | ES6 | JavaScript | TypeScript
10. symbols | ES6 | JavaScript | TypeScript
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part
 
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT
 
Introduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java LibraryIntroduction To Vavr: A Functional Java Library
Introduction To Vavr: A Functional Java Library
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPISlicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPI
 
Getting started with scala cats
Getting started with scala catsGetting started with scala cats
Getting started with scala cats
 
Lecture 3 - ES6 Script Advanced for React-Native
Lecture 3 - ES6 Script Advanced for React-NativeLecture 3 - ES6 Script Advanced for React-Native
Lecture 3 - ES6 Script Advanced for React-Native
 
Javascript omg!
Javascript omg!Javascript omg!
Javascript omg!
 
Mistakes made with string object in java
Mistakes made with string object in javaMistakes made with string object in java
Mistakes made with string object in java
 

Similar to JavaScripts internals #1

JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About JavascriptManish Jangir
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
Journey To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The MachineJourney To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The MachineIrfan Maulana
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep DiveManish Jangir
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java scriptvivek p s
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_iNico Ludwig
 
Learning puppet chapter 2
Learning puppet chapter 2Learning puppet chapter 2
Learning puppet chapter 2Vishal Biyani
 
Master in javascript
Master in javascriptMaster in javascript
Master in javascriptRobbin Zhao
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfranjanadeore1
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part IEugene Lazutkin
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScriptdanwrong
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# DevelopersCory Foy
 
Looking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelopLooking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelopPVS-Studio
 

Similar to JavaScripts internals #1 (20)

JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About Javascript
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
Journey To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The MachineJourney To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The Machine
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Learning puppet chapter 2
Learning puppet chapter 2Learning puppet chapter 2
Learning puppet chapter 2
 
Java script
Java scriptJava script
Java script
 
Master in javascript
Master in javascriptMaster in javascript
Master in javascript
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Metaprogramming JavaScript
Metaprogramming  JavaScriptMetaprogramming  JavaScript
Metaprogramming JavaScript
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
Looking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelopLooking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelop
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

JavaScripts internals #1

  • 1. JavaScripts internals #1 Martin Pernica | @martindeveloper
  • 2. Primitive data types in JS How many are there? JavaScript contains only 3 primitive types – string, number, boolean These types arent objects. JS runtime wraps these types using objects wrappers String(), Number() a Boolean().
  • 3. Primitive data types in JS Every primitive type is immutable. Which means, if you work with variable, everytime will be created copy and original stay untouched – „copy by value“ vs „ copy by reference“.
  • 4. Data types in JS •null •undefined •boolean •number •string •object •symbol (ES6) Null is special, yeah thats mean – buggy. typeof null === "object"; // true
  • 5. Null magic This null bug is in JS for 2 decades and maybe never will be fixed. Because many code using this bug behavior. How can I test value if it is null properly? var a = null; (!a && typeof a === "object"); // true This works because null is “falsy like”.
  • 6. Undefined Undefined is value for variables which are defined (in valid scope) but doesn’t have value yet. var a; typeof a === “undefined"// true But I need check if is variable defined at all and can be accessed from current scope, what now? If you use variable which is not defined or is not accessible from current scope, runtime will fire error ReferenceError.
  • 7. Undefined If you use typeof function on non exist variable, it will return “undefined”. var a; typeof a; // undefined typeof b; // undefined But it will not trigger the error!
  • 8. Note about Objects Objects are not primtive types. They are composite type. Composite type holds primitive types and others composite types. Objects are simple look-tables and compiler break them into primitive types.
  • 9. Scopes Like in others programming language use variables for data storing and scopes for determining when and where can be variable accessed. JavaScript is dynamic language and scoping process can be little bit different than in other languages.
  • 10. Scopes JavaScript (simplified) have: • Engine o Engine must ensure program compilation and execution • Compiler o Compiler will parse code and generate “native” code for Engine • Scope o Big table which collects declared variables and have strict rules for accessing them.
  • 11. Scopes var a = 2; 1. Compiler will break it down into tokens and parse them into token tree. 2. After lexing compiler will do code generation (for engine). 3. When compiler reach part “var a”, it asks scope if variable is exist and is accessible. If true, compiler will skip declaration. If false, compiler will ask scope to declare new variable called “a” in current scope. 4. After that compiler will produce code for engine, to handle part “a = 2”. Engine runs will first ask Scope if “a” is accessible from current scope collection. If true, engine will use this “a” variable. If false, engine will looks into nested scopes. 5. If Engine cant find variable “a” in nested scopes, it will raise error.
  • 12. Scopes When engine needs to look-up for variable “a”, it will use Left hand Side look-up. There is also Right hand Side look-up. LHS is trying to find variable container (simplified – area of memory). RHS is trying to find variable value (simplified – content of memory area). LHS or RHS is not only used when engine working with variable, it is used when working with functions, objects and other things also.
  • 13. Nested scopes Sometimes code is nested in more than one scope. For example – one block is nested inside function and function is nested in object and etc. Engine always trying to look-up every outer scope until found or until when reach last „global“ scope.
  • 14. Nested scopes - errors If RHS look-up fails find variable anywhere, the Engine will throw ReferenceError. But if LHS look-up fails (at global scope), then Scope will create variable in global scope with that name and it will return it to Engine. *This “auto creating variable” behavior is not correct and it will fails when program will run in Strict Mode and it will throw ReferenceError too.
  • 15. Scopes – last words Sometimes you can “cheat” scoping behavior using eval() or with keyword (deprecated). Do not use them! It will cheat your program, you can make more mistakes and it will be much slower (because you are trying to create scope in runtime and without code optimalizations)! Scope look-up stops once it finds the first match. You can have same variables in different scopes, but the „last win“. This behavior is called shadowing. Every function have own scope.
  • 16. Anonymous functions Many JS libraries use anonymous function idiomatic style of code. Anonymous function are easy to write but have some problems: • They don’t have a name – bad debugging and you cant call them recursive But you can write inline function expression, it is anonymous function with name. You don’t have declare them outside, but when you write anonymous function, prepend name and that’s it!
  • 17. Fun fact (new Date()).getMonth(); Result? 8
  • 19. Next? 1. Scope closures 2. Dynamic scope 3. Hoisting 4. Garbage Collector 5. Objects (this) 6. Prototypes 7. ES6 features – let, const …
  • 20. Thank you for your attention! And thank you to authors of great book series „You Dont Know JS“! http://shop.oreilly.com/product/0636920026327.do http://shop.oreilly.com/product/0636920033738.do https://github.com/getify/You-Dont-Know-JS Martin Pernica | @martindeveloper