SlideShare a Scribd company logo
JavaScript Patterns

      Part Two
Error Objects


Built-in's you can throw
 ● Error, SyntaxError, TypeError, RangeError, etc.
 ● can be used with or without 'new'


All have properties
 ● name - of constructor
 ● message - string passed in constructor
 ● and others that are not universally supported


You can 'throw' any object
 ● add 'name' and 'message' to be consistent
 ● plus any other properties you want
 ● preferred method
The console


Use FireBug CommandEditor to experiment

LOG outputs traces
console.log("test", 1, {}, [1,2,3]);


DIR outputs enumerations
console.dir({one:1, two: {three: 3}});


ACCESS
window.name === window['name'];
Minimizing Globals


myglobal = "hello";
 ● makes a property on window


console.log(myglobal);
 ● the global 'this' is implied


console.log(window.myglobal);


console.log(window['myglobal']);


console.log(this.myglobal);
Implied Globals


Any variable you don't declare with var becomes property of global


this is bad
function sum(x,y){
result = x+y;
return result;
}


this is good
function sum(x,y){
var result = x+y;
return result;
}
Implied Globals (cont.)


Implied globals are not real variables, rather properties of the global object


this is bad
function foo(){
var a = b = 0;
}


this is good
function foo(){
var a, b;
a = b = 0;
}
Delete


Global variables, created with var, cannot be deleted


Global properties (declared without var) can be deleted


In general
  ● properties can be deleted
  ● variables can not be deleted
Accessing global object


from anywhere via 'window'


*don't EVER declare a local var using the word 'window'


to access global without using 'window' you can do the following from
anywhere, any level of nested scope:


....(){
    var G = (function(){
        return this;
    }());
Single var pattern


single place to look for all local variables


prevents logical errors: when a var is used before it's defined


helps minimize globals/implied globals


less code to write - only 1 'var' statement


can initialize if you want to
Single var pattern (cont.)


another good example of doing work in single var


can initialize and chain


function updateElement(){
  var element = document.getElementById("result"),
       style = element.style;
}
Variable Hoisting


you can put a 'var' statement anywhere in a function


but you shouldn't


all vars will all act as if they were declared at top of function
Loops


optimizing 'for'


least optimal:
for(var i = 0; i < myarray.length; i++){}


especially bad for dom collections since they are live queries
which are very expensive


better:
for(var i = 0, max = myarray.length; i < max; i++){}
Loops (cont.)


'for in' enumeration


use hasOwnProperty() to filter out the prototype properties


call it from the object you are iterating over
OR
from the prototype of Object
  ● avoids collisions with any redefinition of hasOwnProperty
  ● cached version of this can avoid an iterative long   property lookup
    all the way up the prototype chain
Types

There are 5 primitives:
    1. number
    2. string
    3. boolean
    4. null
    5. undefined


Primitives are NOT objects
     ○ no properties
     ○ no methods
     ○ however....there is temporary conversion


Literals are not necessarily primitives
  ● { } and [ ] are literals - not primitives
  ● "s", true, 3 are literals - are primitives
Types (cont.)


conversion


   ● parseInt(string, radix)
      ○ converts a string to a number
      ○ do not leave out the radix!
          ■ strings that begin with '0' are treated as octal
      ○ there are other ways to convert a string that are faster
but not be able to handle compound strings
like "08 hello"
Literals


{}, [], "", 3, true , / /


advantages over the built-in constructors
 ● more concise
 ● more expressive
 ● less error-prone
 ● emphasizes the fact that objects are mutable hashes, not classes


constructors can be deceitful
 ● new Object("hello") creates an object using the String constructor
Primitives


difference between number primitive and Number wrapper object


the primitive object wrappers have some useful functions,
but the literals are converted at runtime


if you need to augment the value and persist state, then use the wrapper -
primitives can not do this


wrappers without 'new' can be used to convert values to primitives
Ad

More Related Content

What's hot (20)

C# 4.0 dynamic
C# 4.0 dynamicC# 4.0 dynamic
C# 4.0 dynamic
Wiryadi Adidharma
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
oojs
oojsoojs
oojs
Imran shaikh
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
abhay singh
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
gourav kottawar
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
Knoldus Inc.
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享
LearningTech
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
Type conversions
Type conversionsType conversions
Type conversions
sanya6900
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Function overloading
Function overloadingFunction overloading
Function overloading
Ashish Kelwa
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
abhay singh
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
Knoldus Inc.
 
Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享
LearningTech
 
Type conversions
Type conversionsType conversions
Type conversions
sanya6900
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Function overloading
Function overloadingFunction overloading
Function overloading
Ashish Kelwa
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 

Viewers also liked (17)

iOS release engineering
iOS release engineeringiOS release engineering
iOS release engineering
Chris Farrell
 
Clean Code
Clean CodeClean Code
Clean Code
Chris Farrell
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
Android security
Android securityAndroid security
Android security
Chris Farrell
 
Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in Flex
Chris Farrell
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
Code Kata
Code KataCode Kata
Code Kata
Chris Farrell
 
Classic Mistakes
Classic MistakesClassic Mistakes
Classic Mistakes
Chris Farrell
 
JavaScript: The Good Parts
JavaScript: The Good PartsJavaScript: The Good Parts
JavaScript: The Good Parts
Chris Farrell
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
JavaScript: Patterns, Part 1
JavaScript: Patterns, Part  1JavaScript: Patterns, Part  1
JavaScript: Patterns, Part 1
Chris Farrell
 
OpenGL ES on Android
OpenGL ES on AndroidOpenGL ES on Android
OpenGL ES on Android
Chris Farrell
 
Function Points
Function PointsFunction Points
Function Points
Chris Farrell
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development Fundamentals
Chris Farrell
 
iOS: A Broad Overview
iOS: A Broad OverviewiOS: A Broad Overview
iOS: A Broad Overview
Chris Farrell
 
iOS App Dev
iOS App Dev iOS App Dev
iOS App Dev
Chris Farrell
 
iOS release engineering
iOS release engineeringiOS release engineering
iOS release engineering
Chris Farrell
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in Flex
Chris Farrell
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
JavaScript: The Good Parts
JavaScript: The Good PartsJavaScript: The Good Parts
JavaScript: The Good Parts
Chris Farrell
 
Presentation janice baay
Presentation janice baayPresentation janice baay
Presentation janice baay
Janice Baay
 
JavaScript: Patterns, Part 1
JavaScript: Patterns, Part  1JavaScript: Patterns, Part  1
JavaScript: Patterns, Part 1
Chris Farrell
 
OpenGL ES on Android
OpenGL ES on AndroidOpenGL ES on Android
OpenGL ES on Android
Chris Farrell
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development Fundamentals
Chris Farrell
 
iOS: A Broad Overview
iOS: A Broad OverviewiOS: A Broad Overview
iOS: A Broad Overview
Chris Farrell
 
Ad

Similar to JavaScript: Patterns, Part 2 (20)

JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
Ivano Malavolta
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
sandeshshahapur
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Java script
Java scriptJava script
Java script
Adrian Caetano
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time Checks
Stoyan Nikolov
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
App_development55555555555555555555.pptx
App_development55555555555555555555.pptxApp_development55555555555555555555.pptx
App_development55555555555555555555.pptx
sameehamoogab
 
Aspdot
AspdotAspdot
Aspdot
Nishad Nizarudeen
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to Programming
Cindy Royal
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17
Daniel Eriksson
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
Bala Narayanan
 
lecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.pptlecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.ppt
ULADATZ
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
Raghu nath
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
Christian Heilmann
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
Tarek Yehia
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
datamantra
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
Núcleo de Electrónica e Informática da Universidade do Algarve
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
Mudit Gupta
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
sandeshshahapur
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time Checks
Stoyan Nikolov
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
App_development55555555555555555555.pptx
App_development55555555555555555555.pptxApp_development55555555555555555555.pptx
App_development55555555555555555555.pptx
sameehamoogab
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to Programming
Cindy Royal
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17
Daniel Eriksson
 
lecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.pptlecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.ppt
ULADATZ
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
Raghu nath
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
Tarek Yehia
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
datamantra
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
Mudit Gupta
 
Ad

Recently uploaded (20)

Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Unlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive GuideUnlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive Guide
vikasascentbpo
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Social Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTechSocial Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTech
Steve Jonas
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Unlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive GuideUnlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive Guide
vikasascentbpo
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Social Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTechSocial Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTech
Steve Jonas
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 

JavaScript: Patterns, Part 2

  • 2. Error Objects Built-in's you can throw ● Error, SyntaxError, TypeError, RangeError, etc. ● can be used with or without 'new' All have properties ● name - of constructor ● message - string passed in constructor ● and others that are not universally supported You can 'throw' any object ● add 'name' and 'message' to be consistent ● plus any other properties you want ● preferred method
  • 3. The console Use FireBug CommandEditor to experiment LOG outputs traces console.log("test", 1, {}, [1,2,3]); DIR outputs enumerations console.dir({one:1, two: {three: 3}}); ACCESS window.name === window['name'];
  • 4. Minimizing Globals myglobal = "hello"; ● makes a property on window console.log(myglobal); ● the global 'this' is implied console.log(window.myglobal); console.log(window['myglobal']); console.log(this.myglobal);
  • 5. Implied Globals Any variable you don't declare with var becomes property of global this is bad function sum(x,y){ result = x+y; return result; } this is good function sum(x,y){ var result = x+y; return result; }
  • 6. Implied Globals (cont.) Implied globals are not real variables, rather properties of the global object this is bad function foo(){ var a = b = 0; } this is good function foo(){ var a, b; a = b = 0; }
  • 7. Delete Global variables, created with var, cannot be deleted Global properties (declared without var) can be deleted In general ● properties can be deleted ● variables can not be deleted
  • 8. Accessing global object from anywhere via 'window' *don't EVER declare a local var using the word 'window' to access global without using 'window' you can do the following from anywhere, any level of nested scope: ....(){ var G = (function(){ return this; }());
  • 9. Single var pattern single place to look for all local variables prevents logical errors: when a var is used before it's defined helps minimize globals/implied globals less code to write - only 1 'var' statement can initialize if you want to
  • 10. Single var pattern (cont.) another good example of doing work in single var can initialize and chain function updateElement(){ var element = document.getElementById("result"), style = element.style; }
  • 11. Variable Hoisting you can put a 'var' statement anywhere in a function but you shouldn't all vars will all act as if they were declared at top of function
  • 12. Loops optimizing 'for' least optimal: for(var i = 0; i < myarray.length; i++){} especially bad for dom collections since they are live queries which are very expensive better: for(var i = 0, max = myarray.length; i < max; i++){}
  • 13. Loops (cont.) 'for in' enumeration use hasOwnProperty() to filter out the prototype properties call it from the object you are iterating over OR from the prototype of Object ● avoids collisions with any redefinition of hasOwnProperty ● cached version of this can avoid an iterative long property lookup all the way up the prototype chain
  • 14. Types There are 5 primitives: 1. number 2. string 3. boolean 4. null 5. undefined Primitives are NOT objects ○ no properties ○ no methods ○ however....there is temporary conversion Literals are not necessarily primitives ● { } and [ ] are literals - not primitives ● "s", true, 3 are literals - are primitives
  • 15. Types (cont.) conversion ● parseInt(string, radix) ○ converts a string to a number ○ do not leave out the radix! ■ strings that begin with '0' are treated as octal ○ there are other ways to convert a string that are faster but not be able to handle compound strings like "08 hello"
  • 16. Literals {}, [], "", 3, true , / / advantages over the built-in constructors ● more concise ● more expressive ● less error-prone ● emphasizes the fact that objects are mutable hashes, not classes constructors can be deceitful ● new Object("hello") creates an object using the String constructor
  • 17. Primitives difference between number primitive and Number wrapper object the primitive object wrappers have some useful functions, but the literals are converted at runtime if you need to augment the value and persist state, then use the wrapper - primitives can not do this wrappers without 'new' can be used to convert values to primitives