SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
SOME JAVASCRIPT
   PITFALLS
  by Claudio Cicali (Sponsorpay)

  @mi.o / @aui
   galcm / cldo
DISCLAIMER
          The following examples are meant to run
                   inside the global scope

I'm also not a "always use the === test operator" kind of guy [1]



                       [1] long time PHP developer
DECLARATIONS AND EXPRESSIONS
/ Avral dcaain
 /  aibe elrto
vrA
 a ;

/ A asgmn epeso (eiiin/iiilzto)
 / n sinet xrsin dfnto  ntaiain
A=0 ;



/ Dcaainaddfnto
 / elrto n eiiin
vrA=0
 a    ;
DON'T TOUCH UNDECLARED VARIABLES
/ Rfrneerr Ai ntdfnd(utfrtehrls ts)
 / eeec ro:   s o eie js o h ames et
i (A= udfnd){
 f   = neie
  cnoelg"el";
   osl.o(Hlo)
}

/ Ti wl fi a wl (eycmo msae)
 / hs il al s el vr omn itk!
i (! ){
 f  A
  cnoelg"el";
   osl.o(Hlo)
}
DON'T TOUCH UNDECLARED VARIABLES
/ T ts i avral i dcae,awy*uetetpo oeao
 / o et f  aibe s elrd las s h yef prtr
i (tpo A= "neie"){
 f  yef   = udfnd
  cnoelg"el";
   osl.o(hlo)
}

/ *Uls yuaetsiga ojc poet
 / nes o r etn n bet rpry



vrA
 a ;

/ N err
 / o ros
i (A= udfnd){
 f   = neie
  cnoelg A) / "neie"
   osl.o(  ; / udfnd
}
UNDEFINED PROPERTIES
  vrmOj={
   a yb
  };

  / N err (ecnsi tetpo ts)
   / o ros w a kp h yef et
  i (mOjA= udfnd){
   f  yb. = neie
    cnoelg tpo mOjA) / udfnd
     osl.o( yef yb. ; / neie
  }



any object property is declared within the object itself
VARIABLE HOISTING
/ N err
 / o ros
i ( ==0 {
 f A = )
  cnoelg"aeaCk";
   osl.o(Hv  oe)
}

/ I tesm soe vralscnb dcae *fe*bigue
 / n h ae cp, aibe a e elrd atr en sd
vrA=0
 a    ;



    / Tedcaaini "ose"a tevr tpo tesoe
     / h elrto s hitd t h ey o f h cp
+- vrA
 -> a ;
|
|   / Tets fisnntees
     / h et al oehls
|   i ( ==0 {
     f A = )
|     cnoelg"aeaCk";
      osl.o(Hv    oe)
|   }
|
|   / Tedfnto i NThitd(hs n Ck fru)
     / h eiiin s O ose tu, o oe o s
+- A=0
 --     ;
VARIABLE HOISTING
/ Err(elrto oto soe n "osig)
 / ro dcaain u f cp, o hitn"
i (){
 f A
  cnoelg"el";
   osl.o(hlo)
}

fnto foa( {
 ucin obr)
  vrA=0
   a   ;
}



vrA=4;
 a   2

fnto foa( {
 ucin obr)

    cnoelg A) / Cnyuges
     osl.o(  ; / a o us?

    vrA=0 / Dcaainhitd NTteasgmn
     a   ; / elrto ose, O h sinet
}

foa(;
 obr)
VARIABLE HOISTING
vrA=4;
 a   2

fnto foa( {
 ucin obr)

    cnoelg A) / 4
     osl.o(  ; / 2

    / vrA=0
     / a   ;
}

foa(;
 obr)
FUNCTION DECLARATIONS AND
           EXPRESSIONS
/ Ti afnto dcaain( nm,n asgmn)
 / hs ucin elrto a ae o sinet
fnto FOA( {
 ucin OBR)
}

/ Ti i afnto epeso (sinet
 / hs s  ucin xrsin asgmn)
vrFOA =fnto( {
 a OBR   ucin)
};

/ Ti i silafnto epeso ( "ae"oe
 / hs s tl  ucin xrsin a nmd n)
vrFOA =fnto smtig){
 a OBR   ucin oehn(
  / Te"oehn"smo cnb ue ol i ti soe
   / h smtig ybl a e sd ny n hs cp
};
FUNCTION DECLARATIONS AND
           EXPRESSIONS
(ucinsmtig){
 fnto oehn(
 / Rcrinwtotuigagmnscle (erctd
  / euso ihu sn ruet.ale dpeae)
 smtig)
  oehn(;
}(;
 ))

/ Ti i wog(ytxerr
 / hs s rn sna ro)
fnto( {;
 ucin) }

/ Bttoeae-aan-vldepesos
 / u hs r  gi  ai xrsin
-ucin){;
 fnto( }
+ucin){;
 fnto( }
!ucin){;
 fnto( }
0fnto( {;
 ,ucin) }
(ucin){)
 fnto( };
FUNCTION DECLARATION HOISTING
/ Aalbe(elrto hsbe hitd
 / vial dcaain a en ose)
fo) / "n"
 o(; / Oe

/ Fnto dcaain(/ ti w' gta err
 / ucin elrto wo hs ed e n ro)
fnto fo){
 ucin o(
  cnoelg'n';
   osl.o(Oe)
}

/ Fnto epeso (vlae a rntm,nthitd
 / ucin xrsin eautd t u-ie o ose)
vrfo=fnto( {
 a o   ucin)
  cnoelg"w";
   osl.o(To)
}

/ Wl lg"w"
 / il o To
fo)
 o(;
HIDDEN GLOBALS
fnto slyprto( {
 ucin ilOeain)

    vrA=B=0
     a     ;

    / Wl b pre a flos
     / il e asd s olw
    vrA
     a ;

    A=0;
    B=0;

    / tu cetn agoa vral B
     / hs raig lbl aibe

}
HIDDEN GLOBALS
/ Wl cet agoa P
 / il rae  lbl
fr( i mOjc){
 o P n ybet
  cnoelgP;
   osl.o()
}

/ Awy uevr
 / las s a
fr(a Pi mOjc){
 o vr   n ybet
  cnoelgP;
   osl.o()
}
ARRAYS: DO NOT USE AS DICTIONARIES!
vrA=[;
 a   ]

/ Teeaen "soitv ary" i JvSrp
 / hr r o ascaie ras, n aacit

A'e1]=10 / Tesm a Aky =10
 [ky' 0; / h ae s .e1  0;
A'e2]=20 / Tesm a Aky =20
 [ky' 0; / h ae s .e2  0;

cnoelgAlnt) / 0 sil0
 osl.o(.egh; / , tl



/ Uepanojcsised
 / s li bet nta
vrA={;
 a    }

A'e1]=10
 [ky' 0;
A'e2]=20
 [ky' 0;

cnoelg Ojc.esA.egh) / 2(S)
 osl.o( betky()lnt ; / E5
ARRAYS: THEY ARE NOT SPARSE
vrA=[;
 a   ]

A99 =4;
 [9]  7

cnoelg Alnt ) / 10 (ih99udfndvle)
 osl.o( .egh ; / 00 wt 9 neie aus
USE USE STRICT
/ Sic o "titmd"wt js a hrls srn
 / wth n src oe ih ut n ames tig

"s src"
 ue tit;

/ Ti,nwilgl asgmn o a udfndvral wl
 / hs o lea, sinet f n neie aibe il
/ trwa errisedo ipiieydcaiga(lbl vral
 / ho n ro nta f mlctl elrn   goa) aib
e

A=4;
   9



"s src"
 ue tit;

/ Dpdpoete:ti wl nwtrwa err
 / ue rpris hs il o ho n ro
vrA={P:"o" P:"a" P:"o"}
 a    1 fo, 2 br, 1 zt ;
USE USE STRICT
"s src"
 ue tit;

/ "ih de nteitayoe(ytxerr
 / wt" os o xs nmr sna ro)
wt (bet {
 ih ojc)

}



"s src"
 ue tit;

fnto foa( {
 ucin obr)
  cnoelg ti ) / udfnd
   osl.o( hs ; / neie
}
USE USE STRICT
"s src"
 ue tit;
/ "ne fntos aentpritdral AYHR
 / Inr ucin" r o emte ely NWEE
i (re {
 f tu)
  fnto inrobr){} / sna err
   ucin neFoa(    / ytx ro
}
/ I ntsrc-oe yucudwieamntust lk
 / n o titmd, o ol rt   osroiy ie
i (re {
 f tu)
  fnto inrobr){cnoelg1;}
   ucin neFoa(  osl.o()
}es {
   le
  fnto inrobr){cnoelg2;}
   ucin neFoa(  osl.o()
}
inrobr) / Cnyugestersl?
 neFoa(; / a o us h eut

fnto otrobr){
 ucin ueFoa(
  fnto inrobr){} / Silpritd
   ucin neFoa(    / tl emte
}
A NOTE OF WARNING ON USE STRICT
       Strict mode applies to the context it is used
Scrict mode changes semantics: be sure to apply it only on
                    code you control
 <cit
  srp>
 / goa cnet
  / lbl otx
 "s src"
  ue tit;
 ..
  .
 <srp>
  /cit

 <citsc"eaylbj"<srp>
  srp r=lgc_i.s>/cit

 / Nwtecd isd lgc_i wl rni src md.
  / o h oe nie eaylb il u n tit oe
 / Dntd ta.
  / o' o ht
( PARENTHESIS )
/ Ltsgtteitgrpr o ti nme
 / e' e h nee at f hs ubr
vrA=31;
 a    .4

/ Teuulwy
 / h sa a
cnoelg Mt.lo(A))
 osl.o( ahfor   ;

/ Sitl fse,cmrse bte
 / lghy atr opess etr
cnoelg 0A)
 osl.o( | ;
( PARENTHESIS )
/ Wa I"icvrd,uigti tcnqe
 / ht  dsoee" sn hs ehiu
vrA=31;
 a    .4
vrB=31;
 a    .4

cnoelg 0A==0B) / tu?N,""(reih
 osl.o( | = | ; / re o 3 tu-s)



/ M bgwsepotdb smtiglk
 / y u a xlie y oehn ie
vrA=31;
 a    .4
vrB=1.4
 a    31;

cnoelg 0A==0B) / fle N,"3,siltu-s
 osl.o( | = | ; / as? o 1" tl reih
( PARENTHESIS )
           What happened then?
      Operators precedence, of course.
0A==0B/ i 0fle1.4
 | = | / s |as|31

/ We usr,ueprnhss
 / hn nue s aetei

vrA=31;
 a   .4
vrB=1.4
 a   31;

cnoelg (|)==(|)) / fle Ys
 osl.o( 0A = 0B ; / as? e!
IIFE ARE LOVELY. USE THEM.
 Immediately-Invoked Function Expression ("iffy")
;fnto(,$ udfnd {
 (ucinw , neie)

 / Ti i yu cutad hr yucnpa sf adsud
  / hs s or oryr: ee o a ly ae n on

 / Bte cmrsin
  / etr opeso
 / Sihl fse we acsiggoa ojcs(hc aelcl
  / lgty atr hn cesn lbl bet wih r oa)
 / Ntee oesnl goa smo dcae
  / o vn n ige lbl ybl elrd

 "s src" / Src md fryu cd ol
  ue tit; / tit oe o or oe ny

}(idw jur)
 )wno, Qey;
IIFE ARE LOVELY. USE THEM.
               More than one style
/ Du Cokodrcmed ti oe
 / og rcfr eomns hs n
(fnto( {}))
  ucin)   ( ;

/ Ti oewl wr a wl
 / hs n il ok s el
(fnto( { ))
  ucin) } (;



Technically, we need to coerce the function to be an
           expression, not a declaration
       Other "formats" exist, but you better
       stick with those for convention sake.
THANK YOU
Claudio Cicali, @caludio on twitter



          Created with Reveal.js

Weitere ähnliche Inhalte

Was ist angesagt?

Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)Corley S.r.l.
 
EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)
EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)
EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)MorarjiEr
 
Year 8 Shared Sonnet Assessment Marc
Year 8 Shared Sonnet Assessment MarcYear 8 Shared Sonnet Assessment Marc
Year 8 Shared Sonnet Assessment MarcJeremy Tang
 
Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...
Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...
Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...Pravotv
 
Anmol ALUCAST certificate.PDF
Anmol ALUCAST certificate.PDFAnmol ALUCAST certificate.PDF
Anmol ALUCAST certificate.PDFAnmol Rathi
 
Learn Frontend Testing
Learn Frontend TestingLearn Frontend Testing
Learn Frontend TestingRyan Roemer
 
Area, surface &amp; volume
Area, surface &amp; volumeArea, surface &amp; volume
Area, surface &amp; volumeZahidul Islam
 
Children's stories
Children's storiesChildren's stories
Children's storiesKarasunaki
 
Computer Network Defense Course 1
Computer Network Defense Course 1Computer Network Defense Course 1
Computer Network Defense Course 1Mark Gagnon
 
Juzt-Reboot Instant Recovery System Brochure
Juzt-Reboot Instant Recovery System Brochure Juzt-Reboot Instant Recovery System Brochure
Juzt-Reboot Instant Recovery System Brochure Juzt-Reboot
 
1.introduction to electrical circuits
1.introduction to electrical circuits1.introduction to electrical circuits
1.introduction to electrical circuitsVenu Gopal Reddy
 
Stability analysis using Routh-Hurwitz Criterion
Stability analysis using Routh-Hurwitz CriterionStability analysis using Routh-Hurwitz Criterion
Stability analysis using Routh-Hurwitz CriterionVijayalaxmiKumbhar
 

Was ist angesagt? (19)

evyap
evyapevyap
evyap
 
Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)
 
EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)
EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)
EC8452-ELECTRONIC CIRCUITS II (Handwritten Notes)
 
Year 8 Shared Sonnet Assessment Marc
Year 8 Shared Sonnet Assessment MarcYear 8 Shared Sonnet Assessment Marc
Year 8 Shared Sonnet Assessment Marc
 
Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...
Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...
Porivnyalna tablicya do proektu zakonu pro vnesennya zmin do deyakih zakonoda...
 
Anmol ALUCAST certificate.PDF
Anmol ALUCAST certificate.PDFAnmol ALUCAST certificate.PDF
Anmol ALUCAST certificate.PDF
 
Img
ImgImg
Img
 
Learn Frontend Testing
Learn Frontend TestingLearn Frontend Testing
Learn Frontend Testing
 
Area, surface &amp; volume
Area, surface &amp; volumeArea, surface &amp; volume
Area, surface &amp; volume
 
IDJ3
IDJ3IDJ3
IDJ3
 
Children's stories
Children's storiesChildren's stories
Children's stories
 
Computer Network Defense Course 1
Computer Network Defense Course 1Computer Network Defense Course 1
Computer Network Defense Course 1
 
Juzt-Reboot Instant Recovery System Brochure
Juzt-Reboot Instant Recovery System Brochure Juzt-Reboot Instant Recovery System Brochure
Juzt-Reboot Instant Recovery System Brochure
 
Profess
ProfessProfess
Profess
 
1.introduction to electrical circuits
1.introduction to electrical circuits1.introduction to electrical circuits
1.introduction to electrical circuits
 
PMP
PMPPMP
PMP
 
32.28
32.2832.28
32.28
 
H.S.C certificate
H.S.C certificateH.S.C certificate
H.S.C certificate
 
Stability analysis using Routh-Hurwitz Criterion
Stability analysis using Routh-Hurwitz CriterionStability analysis using Routh-Hurwitz Criterion
Stability analysis using Routh-Hurwitz Criterion
 

Andere mochten auch

Node.js – Convincing the boss
Node.js – Convincing the bossNode.js – Convincing the boss
Node.js – Convincing the bossClaudio Cicali
 
Indirizzo ip (edoardo saracino)
Indirizzo ip (edoardo saracino)Indirizzo ip (edoardo saracino)
Indirizzo ip (edoardo saracino)lorena sinigaglia
 
An introduction to the web
An introduction to the webAn introduction to the web
An introduction to the webClaudio Cicali
 

Andere mochten auch (6)

Node.js – Convincing the boss
Node.js – Convincing the bossNode.js – Convincing the boss
Node.js – Convincing the boss
 
Youryoutube
YouryoutubeYouryoutube
Youryoutube
 
Indirizzo ip (edoardo saracino)
Indirizzo ip (edoardo saracino)Indirizzo ip (edoardo saracino)
Indirizzo ip (edoardo saracino)
 
An introduction to the web
An introduction to the webAn introduction to the web
An introduction to the web
 
Drupal + Apache SOLR
Drupal + Apache SOLRDrupal + Apache SOLR
Drupal + Apache SOLR
 
Introduzione a OpenID
Introduzione a OpenIDIntroduzione a OpenID
Introduzione a OpenID
 

Ähnlich wie JavaScript pitfalls

How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...
How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...
How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...Ari Lerner
 
Asynchronous java script
Asynchronous java scriptAsynchronous java script
Asynchronous java scriptRon Martin
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best PracticesJohannes Hoppe
 
A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1jsalonen Salonen
 
Presenting Seq for Node.js
Presenting Seq for Node.jsPresenting Seq for Node.js
Presenting Seq for Node.jsDouglas Muth
 
Academic Transcript Atmajaya
Academic Transcript AtmajayaAcademic Transcript Atmajaya
Academic Transcript AtmajayaYosua Samuel
 
Aksharica - 065 (अक्षरिका - ०६५)
Aksharica - 065 (अक्षरिका - ०६५)Aksharica - 065 (अक्षरिका - ०६५)
Aksharica - 065 (अक्षरिका - ०६५)Rajesh Koirala
 
Aksharica - 061 (अक्षरिका - ०६१)
Aksharica - 061 (अक्षरिका - ०६१)Aksharica - 061 (अक्षरिका - ०६१)
Aksharica - 061 (अक्षरिका - ०६१)Rajesh Koirala
 
Modeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ frModeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ frCédric Brun
 
Create Custom Post Type Plugin
Create Custom Post Type PluginCreate Custom Post Type Plugin
Create Custom Post Type PluginJan Wilson
 
Modeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the worldModeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the worldCédric Brun
 
Aksharica - 112 (अक्षरिका - ११२)
Aksharica - 112 (अक्षरिका - ११२) Aksharica - 112 (अक्षरिका - ११२)
Aksharica - 112 (अक्षरिका - ११२) Rajesh Koirala
 
Aksharica - 114 (अक्षरिका - ११४)
Aksharica - 114 (अक्षरिका - ११४)Aksharica - 114 (अक्षरिका - ११४)
Aksharica - 114 (अक्षरिका - ११४)Rajesh Koirala
 
Aksharica - 117 (अक्षरिका - ११७)
Aksharica - 117 (अक्षरिका - ११७)Aksharica - 117 (अक्षरिका - ११७)
Aksharica - 117 (अक्षरिका - ११७)Rajesh Koirala
 

Ähnlich wie JavaScript pitfalls (20)

Javascript: The Important Bits
Javascript: The Important BitsJavascript: The Important Bits
Javascript: The Important Bits
 
How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...
How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...
How to create a 3.2 billion dollar business in 20 minutes: combining AngularJ...
 
Asynchronous java script
Asynchronous java scriptAsynchronous java script
Asynchronous java script
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
 
A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1
 
Presenting Seq for Node.js
Presenting Seq for Node.jsPresenting Seq for Node.js
Presenting Seq for Node.js
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Bini bigyapan
Bini bigyapanBini bigyapan
Bini bigyapan
 
IMG_0001
IMG_0001IMG_0001
IMG_0001
 
Academic Transcript Atmajaya
Academic Transcript AtmajayaAcademic Transcript Atmajaya
Academic Transcript Atmajaya
 
Aksharica - 065 (अक्षरिका - ०६५)
Aksharica - 065 (अक्षरिका - ०६५)Aksharica - 065 (अक्षरिका - ०६५)
Aksharica - 065 (अक्षरिका - ०६५)
 
Aksharica - 061 (अक्षरिका - ०६१)
Aksharica - 061 (अक्षरिका - ०६१)Aksharica - 061 (अक्षरिका - ०६१)
Aksharica - 061 (अक्षरिका - ०६१)
 
Ff to-fp
Ff to-fpFf to-fp
Ff to-fp
 
Mimi_Kara_Oboeru_N3-Choukai.pdf
Mimi_Kara_Oboeru_N3-Choukai.pdfMimi_Kara_Oboeru_N3-Choukai.pdf
Mimi_Kara_Oboeru_N3-Choukai.pdf
 
Modeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ frModeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ fr
 
Create Custom Post Type Plugin
Create Custom Post Type PluginCreate Custom Post Type Plugin
Create Custom Post Type Plugin
 
Modeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the worldModeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the world
 
Aksharica - 112 (अक्षरिका - ११२)
Aksharica - 112 (अक्षरिका - ११२) Aksharica - 112 (अक्षरिका - ११२)
Aksharica - 112 (अक्षरिका - ११२)
 
Aksharica - 114 (अक्षरिका - ११४)
Aksharica - 114 (अक्षरिका - ११४)Aksharica - 114 (अक्षरिका - ११४)
Aksharica - 114 (अक्षरिका - ११४)
 
Aksharica - 117 (अक्षरिका - ११७)
Aksharica - 117 (अक्षरिका - ११७)Aksharica - 117 (अक्षरिका - ११७)
Aksharica - 117 (अक्षरिका - ११७)
 

JavaScript pitfalls

  • 1. SOME JAVASCRIPT PITFALLS by Claudio Cicali (Sponsorpay) @mi.o / @aui galcm / cldo
  • 2. DISCLAIMER The following examples are meant to run inside the global scope I'm also not a "always use the === test operator" kind of guy [1] [1] long time PHP developer
  • 3. DECLARATIONS AND EXPRESSIONS / Avral dcaain / aibe elrto vrA a ; / A asgmn epeso (eiiin/iiilzto) / n sinet xrsin dfnto ntaiain A=0 ; / Dcaainaddfnto / elrto n eiiin vrA=0 a ;
  • 4. DON'T TOUCH UNDECLARED VARIABLES / Rfrneerr Ai ntdfnd(utfrtehrls ts) / eeec ro: s o eie js o h ames et i (A= udfnd){ f = neie cnoelg"el"; osl.o(Hlo) } / Ti wl fi a wl (eycmo msae) / hs il al s el vr omn itk! i (! ){ f A cnoelg"el"; osl.o(Hlo) }
  • 5. DON'T TOUCH UNDECLARED VARIABLES / T ts i avral i dcae,awy*uetetpo oeao / o et f aibe s elrd las s h yef prtr i (tpo A= "neie"){ f yef = udfnd cnoelg"el"; osl.o(hlo) } / *Uls yuaetsiga ojc poet / nes o r etn n bet rpry vrA a ; / N err / o ros i (A= udfnd){ f = neie cnoelg A) / "neie" osl.o( ; / udfnd }
  • 6. UNDEFINED PROPERTIES vrmOj={ a yb }; / N err (ecnsi tetpo ts) / o ros w a kp h yef et i (mOjA= udfnd){ f yb. = neie cnoelg tpo mOjA) / udfnd osl.o( yef yb. ; / neie } any object property is declared within the object itself
  • 7. VARIABLE HOISTING / N err / o ros i ( ==0 { f A = ) cnoelg"aeaCk"; osl.o(Hv oe) } / I tesm soe vralscnb dcae *fe*bigue / n h ae cp, aibe a e elrd atr en sd vrA=0 a ; / Tedcaaini "ose"a tevr tpo tesoe / h elrto s hitd t h ey o f h cp +- vrA -> a ; | | / Tets fisnntees / h et al oehls | i ( ==0 { f A = ) | cnoelg"aeaCk"; osl.o(Hv oe) | } | | / Tedfnto i NThitd(hs n Ck fru) / h eiiin s O ose tu, o oe o s +- A=0 -- ;
  • 8. VARIABLE HOISTING / Err(elrto oto soe n "osig) / ro dcaain u f cp, o hitn" i (){ f A cnoelg"el"; osl.o(hlo) } fnto foa( { ucin obr) vrA=0 a ; } vrA=4; a 2 fnto foa( { ucin obr) cnoelg A) / Cnyuges osl.o( ; / a o us? vrA=0 / Dcaainhitd NTteasgmn a ; / elrto ose, O h sinet } foa(; obr)
  • 9. VARIABLE HOISTING vrA=4; a 2 fnto foa( { ucin obr) cnoelg A) / 4 osl.o( ; / 2 / vrA=0 / a ; } foa(; obr)
  • 10. FUNCTION DECLARATIONS AND EXPRESSIONS / Ti afnto dcaain( nm,n asgmn) / hs ucin elrto a ae o sinet fnto FOA( { ucin OBR) } / Ti i afnto epeso (sinet / hs s ucin xrsin asgmn) vrFOA =fnto( { a OBR ucin) }; / Ti i silafnto epeso ( "ae"oe / hs s tl ucin xrsin a nmd n) vrFOA =fnto smtig){ a OBR ucin oehn( / Te"oehn"smo cnb ue ol i ti soe / h smtig ybl a e sd ny n hs cp };
  • 11. FUNCTION DECLARATIONS AND EXPRESSIONS (ucinsmtig){ fnto oehn( / Rcrinwtotuigagmnscle (erctd / euso ihu sn ruet.ale dpeae) smtig) oehn(; }(; )) / Ti i wog(ytxerr / hs s rn sna ro) fnto( {; ucin) } / Bttoeae-aan-vldepesos / u hs r gi ai xrsin -ucin){; fnto( } +ucin){; fnto( } !ucin){; fnto( } 0fnto( {; ,ucin) } (ucin){) fnto( };
  • 12. FUNCTION DECLARATION HOISTING / Aalbe(elrto hsbe hitd / vial dcaain a en ose) fo) / "n" o(; / Oe / Fnto dcaain(/ ti w' gta err / ucin elrto wo hs ed e n ro) fnto fo){ ucin o( cnoelg'n'; osl.o(Oe) } / Fnto epeso (vlae a rntm,nthitd / ucin xrsin eautd t u-ie o ose) vrfo=fnto( { a o ucin) cnoelg"w"; osl.o(To) } / Wl lg"w" / il o To fo) o(;
  • 13. HIDDEN GLOBALS fnto slyprto( { ucin ilOeain) vrA=B=0 a ; / Wl b pre a flos / il e asd s olw vrA a ; A=0; B=0; / tu cetn agoa vral B / hs raig lbl aibe }
  • 14. HIDDEN GLOBALS / Wl cet agoa P / il rae lbl fr( i mOjc){ o P n ybet cnoelgP; osl.o() } / Awy uevr / las s a fr(a Pi mOjc){ o vr n ybet cnoelgP; osl.o() }
  • 15. ARRAYS: DO NOT USE AS DICTIONARIES! vrA=[; a ] / Teeaen "soitv ary" i JvSrp / hr r o ascaie ras, n aacit A'e1]=10 / Tesm a Aky =10 [ky' 0; / h ae s .e1 0; A'e2]=20 / Tesm a Aky =20 [ky' 0; / h ae s .e2 0; cnoelgAlnt) / 0 sil0 osl.o(.egh; / , tl / Uepanojcsised / s li bet nta vrA={; a } A'e1]=10 [ky' 0; A'e2]=20 [ky' 0; cnoelg Ojc.esA.egh) / 2(S) osl.o( betky()lnt ; / E5
  • 16. ARRAYS: THEY ARE NOT SPARSE vrA=[; a ] A99 =4; [9] 7 cnoelg Alnt ) / 10 (ih99udfndvle) osl.o( .egh ; / 00 wt 9 neie aus
  • 17. USE USE STRICT / Sic o "titmd"wt js a hrls srn / wth n src oe ih ut n ames tig "s src" ue tit; / Ti,nwilgl asgmn o a udfndvral wl / hs o lea, sinet f n neie aibe il / trwa errisedo ipiieydcaiga(lbl vral / ho n ro nta f mlctl elrn goa) aib e A=4; 9 "s src" ue tit; / Dpdpoete:ti wl nwtrwa err / ue rpris hs il o ho n ro vrA={P:"o" P:"a" P:"o"} a 1 fo, 2 br, 1 zt ;
  • 18. USE USE STRICT "s src" ue tit; / "ih de nteitayoe(ytxerr / wt" os o xs nmr sna ro) wt (bet { ih ojc) } "s src" ue tit; fnto foa( { ucin obr) cnoelg ti ) / udfnd osl.o( hs ; / neie }
  • 19. USE USE STRICT "s src" ue tit; / "ne fntos aentpritdral AYHR / Inr ucin" r o emte ely NWEE i (re { f tu) fnto inrobr){} / sna err ucin neFoa( / ytx ro } / I ntsrc-oe yucudwieamntust lk / n o titmd, o ol rt osroiy ie i (re { f tu) fnto inrobr){cnoelg1;} ucin neFoa( osl.o() }es { le fnto inrobr){cnoelg2;} ucin neFoa( osl.o() } inrobr) / Cnyugestersl? neFoa(; / a o us h eut fnto otrobr){ ucin ueFoa( fnto inrobr){} / Silpritd ucin neFoa( / tl emte }
  • 20. A NOTE OF WARNING ON USE STRICT Strict mode applies to the context it is used Scrict mode changes semantics: be sure to apply it only on code you control <cit srp> / goa cnet / lbl otx "s src" ue tit; .. . <srp> /cit <citsc"eaylbj"<srp> srp r=lgc_i.s>/cit / Nwtecd isd lgc_i wl rni src md. / o h oe nie eaylb il u n tit oe / Dntd ta. / o' o ht
  • 21. ( PARENTHESIS ) / Ltsgtteitgrpr o ti nme / e' e h nee at f hs ubr vrA=31; a .4 / Teuulwy / h sa a cnoelg Mt.lo(A)) osl.o( ahfor ; / Sitl fse,cmrse bte / lghy atr opess etr cnoelg 0A) osl.o( | ;
  • 22. ( PARENTHESIS ) / Wa I"icvrd,uigti tcnqe / ht dsoee" sn hs ehiu vrA=31; a .4 vrB=31; a .4 cnoelg 0A==0B) / tu?N,""(reih osl.o( | = | ; / re o 3 tu-s) / M bgwsepotdb smtiglk / y u a xlie y oehn ie vrA=31; a .4 vrB=1.4 a 31; cnoelg 0A==0B) / fle N,"3,siltu-s osl.o( | = | ; / as? o 1" tl reih
  • 23. ( PARENTHESIS ) What happened then? Operators precedence, of course. 0A==0B/ i 0fle1.4 | = | / s |as|31 / We usr,ueprnhss / hn nue s aetei vrA=31; a .4 vrB=1.4 a 31; cnoelg (|)==(|)) / fle Ys osl.o( 0A = 0B ; / as? e!
  • 24. IIFE ARE LOVELY. USE THEM. Immediately-Invoked Function Expression ("iffy") ;fnto(,$ udfnd { (ucinw , neie) / Ti i yu cutad hr yucnpa sf adsud / hs s or oryr: ee o a ly ae n on / Bte cmrsin / etr opeso / Sihl fse we acsiggoa ojcs(hc aelcl / lgty atr hn cesn lbl bet wih r oa) / Ntee oesnl goa smo dcae / o vn n ige lbl ybl elrd "s src" / Src md fryu cd ol ue tit; / tit oe o or oe ny }(idw jur) )wno, Qey;
  • 25. IIFE ARE LOVELY. USE THEM. More than one style / Du Cokodrcmed ti oe / og rcfr eomns hs n (fnto( {})) ucin) ( ; / Ti oewl wr a wl / hs n il ok s el (fnto( { )) ucin) } (; Technically, we need to coerce the function to be an expression, not a declaration Other "formats" exist, but you better stick with those for convention sake.
  • 26. THANK YOU Claudio Cicali, @caludio on twitter Created with Reveal.js