SlideShare a Scribd company logo
Understanding
                                JavaScript




Thursday, September 20, 12
Why JavaScript?

                     Simple
                     Multi paradigm
                     Works on the server and the browser
                     Lots of libraries
                     JSON


Thursday, September 20, 12
I heard it sucks, its broken

                     Implicit globals
                     Confusing this keyword
                     Confusing OO features, lack of class
                     syntax
                     Type coercion


Thursday, September 20, 12
The good parts




Thursday, September 20, 12
Type system

                     Weak typing means that a language
                     implicitly converts types when used.
                     A programming language is said to use
                     dynamic typing when type checking is
                     performed during run-time as opposed
                     to compile-time.


Thursday, September 20, 12
Type system
                     Constructors              Literals            typeof
                Object()                       {a: ‘b’}           ‘object’

                    Array()                [0, 1, ‘foo’, 3]       ‘object’

                    RegExp()                   /foo.*/            ‘object’

                    Date()                           -            ‘object’

                    -                            null             ‘object’

                Boolean()                    true        false    ‘boolean’

                String()                     “foo”       ‘bar’     string’

                Number()              30     0.5 Infinity NaN     ‘number’

                Function()             function foo() {}         ‘function’

                -                             undefined          ‘undefined’

Thursday, September 20, 12
Type system: Falsy values

                 0
                 null
                 undefined
                 false
                 ""
                 NaN
Thursday, September 20, 12
Type system: Coercion
                 undefined == undefined
                 ",,," == new Array(4)
                 NaN != NaN
                 "wat" - 1 == NaN
                 {} + {} == NaN
                 [] + [] == “”
                 [] + {} == “[object Object]”


Thursday, September 20, 12
Type system




Thursday, September 20, 12
Type system

                 Avoid coercion
                 Define API’s with clear types
                 Use the === operator
                 Read the spec or this article http://
                 webreflection.blogspot.com.es/2010/10/
                 javascript-coercion-demystified.html


Thursday, September 20, 12
Type system
           /**
             * Adds two numbers
             *
             * @param {Number} a
             * @param {Number} b
             * @return {Number}
             */
           function add(a, b) {
               return a + b;
           }

           /**
             * Returns the sumatory of a list of numbers
             *
             * @param {Array<Number>} list
             * @return {Number}
             */
           function sum(list) {
               return list.reduce(add, 0);
           }

Thursday, September 20, 12
Type system: Casting


                 Use the constructors       Number(‘3’) === 3


                 Use the prefix + operator   +‘3’ === 3


                 Use the infix + operator    3 + ‘0’ === ’30’


                 Use the prefix ! operator   !!1 === true




Thursday, September 20, 12
Variables

                 Variables are function scoped
                 The var operator is evaluated statically
                             It instantiates all the variables on the
                             current scope
                             It assigns them the undefined value
                 Assignment on variables that have not
                 been instantiated create a global
Thursday, September 20, 12
Variables
         // Hoisting: Cannot read property 'name' of undefined
         var name = user.name
           , user = {name: 'John'};

         // This creates a global!
         foo = ‘bar’;

         // Function scope
         var a = 10;

         if (true) {
           var a = 20;
         }

         (function () {
           var a = 30;
         }());

         a == 20;


Thursday, September 20, 12
Functions: Declaration vs Expression


         // Function declaration (static)
         function add(a, b) {
           return a + b;
         }

         // Function expression (runtime)
         var add = function (a, b) {
           return a + b;
         }

         // Function named expression (runtime)
         var recursiveAdd = function inc(a, b) {
           if (a > 100) return a;
           return inc(a + b, b);
         }




Thursday, September 20, 12
Functions: Higher order
                 Functions can accept functions as a
                 parameters and can return functions
                 Functions are objects after all, they can
                 even have attributes!
         // Returns a function that will have a delay
         function delayFunction(fn, delay) {
           fn.delayed = true;

           return function () {
              setTimeout(fn, delay);
           };
         });


Thursday, September 20, 12
Functions: Closures

                 Closures are function that “remember”
                 the variables on their scope
         // Gets a function to inspect the given object
         function getInspector(obj) {
            return function (attr) {
               return obj[attr];
            };
         };

         var inspect = getInspector({name: ‘john’, age: 21});
         [‘name’, ‘age’].map(inspect) == [‘john’, 21];




Thursday, September 20, 12
OOP: Prototypes

                 Each object can have a pointer to another
                 object called prototype
                 When we read an attribute from an
                 object but its not present, it will try to
                 find it through the prototype chain
                 Prototypes are powerful but can be
                 confusing. Delegation is easy.

Thursday, September 20, 12
OOP: Prototypes

         // Using non-standard __proto__
         var animal = {eat: true}
           , rabbit = {jump: true};

         rabbit.__proto__ = animal;
         rabbit.jump === true
         rabbit.eat === true

         // Using Object.create
         var animal = {eat: true}
           , rabbit;

         rabbit = Object.create(animal);
         rabbit.jump = true;

         rabbit.eat === true
         rabbit.jump === true


Thursday, September 20, 12
OOP: Constructors
                 Calling a function with the new operator
                 makes it behave like a constructor
                        The keyword this will point to the newl
                        object
                        The constructor will have an implicit
                        return of the new object
                        The pointer to the prototype is
                        assigned to the new object
Thursday, September 20, 12
OOP: Constructors

         var animal = {eats: true};

         function Rabbit(name) {
           this.name = name;
           this.jumps = true;
         }

         Rabbit.prototype = animal;

         var rabbit = new Rabbit('John')

         rabbit.eats === true
         rabbit.jumps === true
         rabbit.name === ‘John’




Thursday, September 20, 12
OOP: The `this` keyword

                 The global object if its on the main scope
                 The parent object of a method if the
                 function is called as a method
                 The newly created object from a
                 constructor called with the new operator
                 The first argument passed to call or
                 apply inside function code
Thursday, September 20, 12
OOP: Constructors

         // global
         this.Boolean = function () {return false;};
         Boolean(2) === false

         // method invocation
         var button = {
            toggle: function () {
              this.enabled = !!this.enabled;
            }
         };
         button.toggle();
         button.enabled === true;

         var toggle = button.toggle;
         toggle();
         button.enabled === true;




Thursday, September 20, 12
OOP: Constructors
         // Constructors
         function Rabbit(name) {
           this.name = name;
         }
         var rabbit = new Rabbit('John')
         rabbit.name === ‘John’;

         var rabbit = Rabbit(‘John’);
         rabbit.name === undefined;
         window.name === ‘John’;

         // call or apply
         [].reduce.call(
            "Javascript is cool!"
         , function (memo, a) {
              return a + memo;
            }
         );


Thursday, September 20, 12
Semicolons



                 Use them all the time
                 If a cool kid trolls you for using them,
                 send them this link http://asi.qfox.nl/



Thursday, September 20, 12

More Related Content

What's hot

Java Performance MythBusters
Java Performance MythBustersJava Performance MythBusters
Java Performance MythBusters
Sebastian Zarnekow
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
Doeun KOCH
 
Day 1
Day 1Day 1
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
Razvan Cojocaru
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
Enrique Juan de Dios
 
LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기
Wanbok Choi
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
Tomohiro Kumagai
 
ECMA 入门
ECMA 入门ECMA 入门
ECMA 入门
chencheng 云谦
 
Obvious Secrets of JavaScript
Obvious Secrets of JavaScriptObvious Secrets of JavaScript
Obvious Secrets of JavaScript
Dmitry Baranovskiy
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
Eelco Visser
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
Daniel Cousineau
 
Akka tips
Akka tipsAkka tips
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
Prajwala Manchikatla
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Sanjeev_Knoldus
 
Suit case class
Suit case classSuit case class
Suit case class
Didier Plaindoux
 
Functions, Types, Programs and Effects
Functions, Types, Programs and EffectsFunctions, Types, Programs and Effects
Functions, Types, Programs and Effects
Raymond Roestenburg
 
Akka in-action
Akka in-actionAkka in-action
Akka in-action
Raymond Roestenburg
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
hwilming
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does not
Sergey Bandysik
 

What's hot (20)

Java Performance MythBusters
Java Performance MythBustersJava Performance MythBusters
Java Performance MythBusters
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
Day 1
Day 1Day 1
Day 1
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
 
ECMA 入门
ECMA 入门ECMA 入门
ECMA 入门
 
Obvious Secrets of JavaScript
Obvious Secrets of JavaScriptObvious Secrets of JavaScript
Obvious Secrets of JavaScript
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
 
Akka tips
Akka tipsAkka tips
Akka tips
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Suit case class
Suit case classSuit case class
Suit case class
 
Functions, Types, Programs and Effects
Functions, Types, Programs and EffectsFunctions, Types, Programs and Effects
Functions, Types, Programs and Effects
 
Akka in-action
Akka in-actionAkka in-action
Akka in-action
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does not
 

Similar to Understanding JavaScript

Introduction To Javascript
Introduction To JavascriptIntroduction To Javascript
Introduction To Javascript
Rajat Pandit
 
Js in the open
Js in the openJs in the open
Js in the open
Victor Porof
 
Proxies are Awesome!
Proxies are Awesome!Proxies are Awesome!
Proxies are Awesome!
Brendan Eich
 
03 Advanced JavaScript
03 Advanced JavaScript03 Advanced JavaScript
03 Advanced JavaScript
Ynon Perek
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
Rodica Dada
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
JyothiAmpally
 
Javascript
JavascriptJavascript
Javascript
Aditya Gaur
 
Javascript And J Query
Javascript And J QueryJavascript And J Query
Javascript And J Query
itsarsalan
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
The basics of Ember Objects
The basics of Ember ObjectsThe basics of Ember Objects
The basics of Ember Objects
Jason Schmidt
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
Thomas Kjeldahl Nilsson
 
Testing javascriptwithjasmine sydjs
Testing javascriptwithjasmine sydjsTesting javascriptwithjasmine sydjs
Testing javascriptwithjasmine sydjs
Jo Cranford
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
Teksify
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action script
Christophe Herreman
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
Giovanni Lodi
 
Get started with YUI
Get started with YUIGet started with YUI
Get started with YUI
Adam Lu
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
JavaScript Object API
JavaScript Object APIJavaScript Object API
JavaScript Object API
SRINIVAS KOLAPARTHI
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 

Similar to Understanding JavaScript (20)

Introduction To Javascript
Introduction To JavascriptIntroduction To Javascript
Introduction To Javascript
 
Js in the open
Js in the openJs in the open
Js in the open
 
Proxies are Awesome!
Proxies are Awesome!Proxies are Awesome!
Proxies are Awesome!
 
03 Advanced JavaScript
03 Advanced JavaScript03 Advanced JavaScript
03 Advanced JavaScript
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
Javascript
JavascriptJavascript
Javascript
 
Javascript And J Query
Javascript And J QueryJavascript And J Query
Javascript And J Query
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
The basics of Ember Objects
The basics of Ember ObjectsThe basics of Ember Objects
The basics of Ember Objects
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
Testing javascriptwithjasmine sydjs
Testing javascriptwithjasmine sydjsTesting javascriptwithjasmine sydjs
Testing javascriptwithjasmine sydjs
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action script
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
 
Get started with YUI
Get started with YUIGet started with YUI
Get started with YUI
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
JavaScript Object API
JavaScript Object APIJavaScript Object API
JavaScript Object API
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 

Recently uploaded

AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 

Recently uploaded (20)

AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 

Understanding JavaScript

  • 1. Understanding JavaScript Thursday, September 20, 12
  • 2. Why JavaScript? Simple Multi paradigm Works on the server and the browser Lots of libraries JSON Thursday, September 20, 12
  • 3. I heard it sucks, its broken Implicit globals Confusing this keyword Confusing OO features, lack of class syntax Type coercion Thursday, September 20, 12
  • 4. The good parts Thursday, September 20, 12
  • 5. Type system Weak typing means that a language implicitly converts types when used. A programming language is said to use dynamic typing when type checking is performed during run-time as opposed to compile-time. Thursday, September 20, 12
  • 6. Type system Constructors Literals typeof Object() {a: ‘b’} ‘object’ Array() [0, 1, ‘foo’, 3] ‘object’ RegExp() /foo.*/ ‘object’ Date() - ‘object’ - null ‘object’ Boolean() true false ‘boolean’ String() “foo” ‘bar’ string’ Number() 30 0.5 Infinity NaN ‘number’ Function() function foo() {} ‘function’ - undefined ‘undefined’ Thursday, September 20, 12
  • 7. Type system: Falsy values 0 null undefined false "" NaN Thursday, September 20, 12
  • 8. Type system: Coercion undefined == undefined ",,," == new Array(4) NaN != NaN "wat" - 1 == NaN {} + {} == NaN [] + [] == “” [] + {} == “[object Object]” Thursday, September 20, 12
  • 10. Type system Avoid coercion Define API’s with clear types Use the === operator Read the spec or this article http:// webreflection.blogspot.com.es/2010/10/ javascript-coercion-demystified.html Thursday, September 20, 12
  • 11. Type system /** * Adds two numbers * * @param {Number} a * @param {Number} b * @return {Number} */ function add(a, b) { return a + b; } /** * Returns the sumatory of a list of numbers * * @param {Array<Number>} list * @return {Number} */ function sum(list) { return list.reduce(add, 0); } Thursday, September 20, 12
  • 12. Type system: Casting Use the constructors Number(‘3’) === 3 Use the prefix + operator +‘3’ === 3 Use the infix + operator 3 + ‘0’ === ’30’ Use the prefix ! operator !!1 === true Thursday, September 20, 12
  • 13. Variables Variables are function scoped The var operator is evaluated statically It instantiates all the variables on the current scope It assigns them the undefined value Assignment on variables that have not been instantiated create a global Thursday, September 20, 12
  • 14. Variables // Hoisting: Cannot read property 'name' of undefined var name = user.name , user = {name: 'John'}; // This creates a global! foo = ‘bar’; // Function scope var a = 10; if (true) { var a = 20; } (function () { var a = 30; }()); a == 20; Thursday, September 20, 12
  • 15. Functions: Declaration vs Expression // Function declaration (static) function add(a, b) { return a + b; } // Function expression (runtime) var add = function (a, b) { return a + b; } // Function named expression (runtime) var recursiveAdd = function inc(a, b) { if (a > 100) return a; return inc(a + b, b); } Thursday, September 20, 12
  • 16. Functions: Higher order Functions can accept functions as a parameters and can return functions Functions are objects after all, they can even have attributes! // Returns a function that will have a delay function delayFunction(fn, delay) { fn.delayed = true; return function () { setTimeout(fn, delay); }; }); Thursday, September 20, 12
  • 17. Functions: Closures Closures are function that “remember” the variables on their scope // Gets a function to inspect the given object function getInspector(obj) { return function (attr) { return obj[attr]; }; }; var inspect = getInspector({name: ‘john’, age: 21}); [‘name’, ‘age’].map(inspect) == [‘john’, 21]; Thursday, September 20, 12
  • 18. OOP: Prototypes Each object can have a pointer to another object called prototype When we read an attribute from an object but its not present, it will try to find it through the prototype chain Prototypes are powerful but can be confusing. Delegation is easy. Thursday, September 20, 12
  • 19. OOP: Prototypes // Using non-standard __proto__ var animal = {eat: true} , rabbit = {jump: true}; rabbit.__proto__ = animal; rabbit.jump === true rabbit.eat === true // Using Object.create var animal = {eat: true} , rabbit; rabbit = Object.create(animal); rabbit.jump = true; rabbit.eat === true rabbit.jump === true Thursday, September 20, 12
  • 20. OOP: Constructors Calling a function with the new operator makes it behave like a constructor The keyword this will point to the newl object The constructor will have an implicit return of the new object The pointer to the prototype is assigned to the new object Thursday, September 20, 12
  • 21. OOP: Constructors var animal = {eats: true}; function Rabbit(name) { this.name = name; this.jumps = true; } Rabbit.prototype = animal; var rabbit = new Rabbit('John') rabbit.eats === true rabbit.jumps === true rabbit.name === ‘John’ Thursday, September 20, 12
  • 22. OOP: The `this` keyword The global object if its on the main scope The parent object of a method if the function is called as a method The newly created object from a constructor called with the new operator The first argument passed to call or apply inside function code Thursday, September 20, 12
  • 23. OOP: Constructors // global this.Boolean = function () {return false;}; Boolean(2) === false // method invocation var button = { toggle: function () { this.enabled = !!this.enabled; } }; button.toggle(); button.enabled === true; var toggle = button.toggle; toggle(); button.enabled === true; Thursday, September 20, 12
  • 24. OOP: Constructors // Constructors function Rabbit(name) { this.name = name; } var rabbit = new Rabbit('John') rabbit.name === ‘John’; var rabbit = Rabbit(‘John’); rabbit.name === undefined; window.name === ‘John’; // call or apply [].reduce.call( "Javascript is cool!" , function (memo, a) { return a + memo; } ); Thursday, September 20, 12
  • 25. Semicolons Use them all the time If a cool kid trolls you for using them, send them this link http://asi.qfox.nl/ Thursday, September 20, 12