SlideShare a Scribd company logo
1 of 19
Download to read offline
Most Common JavaScriptMistakes
Scope «this» sample 
functionTimer (){ 
this.seconds=0; 
this.increment=function(){ 
this.seconds++; 
}; 
setInterval(function(){ 
this.increment(); 
}, 1000); 
} 
vartimer=newTimer();
Scope «this» error 
functionTimer (){ 
this.seconds=0; 
this.increment=function(){ 
this.seconds++; 
}; 
setInterval(function(){ 
this.increment(); 
}, 1000); 
} 
vartimer=newTimer(); 
Uncaught TypeError: undefined is not a function
Scope «this» solution 
functionTimer (){ 
this.seconds=0; 
this.increment=function(){ 
this.seconds++; 
}; 
varself = this; 
setInterval(function(){ 
self.increment(); 
}, 1000); 
} 
vartimer =newTimer();
Bindsample 
varHuman =function( name ){ 
this.name =name; 
}; 
Human.prototype.getName=function(){ 
console.log( this.name ); 
}; 
varhomer =newHuman('Homer'); 
homer.getName(); 
setTimeout( function(){ 
homer.getName(); 
} , 500); 
setTimeout( homer.getName, 500);
Bindoutputs 
varHuman =function( name ){ 
this.name =name; 
}; 
Human.prototype.getName=function(){ 
console.log( this.name ); 
}; 
varhomer =newHuman('Homer'); 
homer.getName(); // Homer 
setTimeout( function(){ 
homer.getName(); // Homer 
} , 500); 
setTimeout( homer.getName, 500); // Empty
Bindsolution 
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. 
varHuman =function( name ){ 
this.name =name; 
}; 
Human.prototype.getName=function(){ 
console.log( this.name ); 
}; 
varhomer =newHuman('Homer'); 
homer.getName(); // Homer 
setTimeout( function(){ 
homer.getName(); // Homer 
} , 500); 
setTimeout( homer.getName.bind(homer) , 500); // Homer
Prototype 
// Shape -superclass 
functionShape() { 
this.x=0; 
this.y=0; 
} 
// superclass method 
Shape.prototype.move=function(x, y) { 
this.x+=x; 
this.y+=y; 
console.info('Shape moved.'); 
}; 
// Rectangle -subclass 
functionRectangle() { 
Shape.call(this); // call super constructor. 
} 
// subclass extends superclass 
Rectangle.prototype=Object.create(Shape.prototype); 
Rectangle.prototype.constructor=Rectangle; 
varrect=newRectangle(); 
console.log(rectinstanceofRectangle); 
console.log(rectinstanceofShape); 
rect.move(1, 1);
Inheritedpropertiessample 
// Shape -superclass 
functionShape() { 
this.x=0; 
this.y=0; 
} 
// superclass method 
Shape.prototype.move=function(x, y) { 
this.x+=x; 
this.y+=y; 
console.info('Shape moved.'); 
}; 
// Rectangle -subclass 
functionRectangle() { 
Shape.call(this); // call super constructor. 
} 
// subclass extends superclass 
Rectangle.prototype=Object.create(Shape.prototype); 
Rectangle.prototype.constructor=Rectangle; 
Rectangle.prototype.toString=function(){ 
return"Rectangle"; 
}; 
varrect=newRectangle(); 
for(varprop inrect){ 
console.log(prop); 
} 
for(varprop inrect){ 
if(rect.hasOwnProperty(prop)){ 
console.log("> "+prop); 
} 
}
Inheritedpropertiesoutputs 
// Shape -superclass 
functionShape() { 
this.x=0; 
this.y=0; 
} 
// superclass method 
Shape.prototype.move=function(x, y) { 
this.x+=x; 
this.y+=y; 
console.info('Shape moved.'); 
}; 
// Rectangle -subclass 
functionRectangle() { 
Shape.call(this); // call super constructor. 
} 
// subclass extends superclass 
Rectangle.prototype=Object.create(Shape.prototype); 
Rectangle.prototype.constructor=Rectangle; 
Rectangle.prototype.toString=function(){ 
return"Rectangle"; 
}; 
varrect=newRectangle(); 
for(varprop inrect){ 
console.log(prop); 
} 
// x, y, move, constructor, toString 
for(varprop inrect){ 
if(rect.hasOwnProperty(prop)){ 
console.log("> "+prop); 
} 
} 
// x, y,
Comparaison sample 
console.log(''==0); 
console.log('0'==0); 
console.log(" trn"==0); 
console.log([] ==0); 
console.log(undefined==null); 
console.log({} ==0); 
console.log(undefined==0); 
if([]) { console.log('true'); } 
if({}) { console.log('true'); } 
console.log(NaN==NaN); 
console.log(NaN===NaN); 
console.log(isNaN(NaN)); 
console.log(''===0); 
console.log('0'===0); 
console.log({} ===0); 
console.log(undefined===null);
Comparaison solution 
console.log(''==0); // true 
console.log('0'==0); // true 
console.log(" trn"==0); // true 
console.log([] ==0); // true 
console.log(undefined==null);// true 
console.log({} ==0); // false 
console.log(undefined==0); // false 
if([]) { console.log('true'); } // true 
if({}) { console.log('true'); }// true 
console.log(NaN==NaN); // false 
console.log(NaN===NaN); // false 
console.log(isNaN(NaN)); // true 
console.log(''===0); // false 
console.log('0'===0); // false 
console.log({} ===0); // false 
console.log(undefined===null);// false
IIFE sample 
var x = 1; 
var y = 10; 
var m = (function(){ 
var x=2; 
y = 11; 
var z = 101; 
return 1000; 
})(); 
console.log(x); 
console.log(y); 
console.log(m); 
console.log(z);
IIFE outputs 
var x = 1; 
var y = 10; 
var m = (function(){ 
var x=2; 
y = 11; 
var z = 101; 
return 1000; 
})(); 
console.log(x); // 1 
console.log(y); // 11 
console.log(m); // 1000 
console.log(z); //z is not defined 
An immediately-invoked function expression (IIFI) is a JavaScript design pattern which produces a lexical scope using JavaScript's function scoping.
Asynchronousloopsample 
for(vari=0; i<3; i++){ 
setTimeout(function(){ 
console.log(i); 
}); 
}
Asynchronousloopproblem 
for(vari=0; i<3; i++){ 
setTimeout(function(){ 
console.log(i); 
}); 
} 
// 3, 3, 3
Asynchronousloopsolution 
for(vari=0; i<3; i++){ 
(function(value){ 
setTimeout(function(){ 
console.log(value); 
}); 
})(i); 
} 
// 0, 1, 2
Variables copy sample 
varme ={ 
name :'Yoann', 
male :true, 
xp:7, 
skills:{ 
java :5, 
js:5, 
android:5 
}, 
toString:function(){ 
return'My name is '+this.name; 
} 
}; 
varclone ={}; 
for(varprop inme){ 
clone[prop] =me[prop]; 
} 
clone.xp=8; 
clone.male=false; 
clone.name ="Nexus-6"; 
clone.skills.dotNet=1; 
deleteclone.skills.android; 
clone.toString=function(){ 
return"My clone name is "+this.name; 
}; 
console.log(me); 
console.log(me.toString());
Variables copy outputs 
{ 
name: 'Yoann', 
male : true, 
xp: 7, 
skills: { 
java : 5, 
js: 5, 
dotNet: 1 
}, 
toString: function(){ 
return 'Mynameis'+this.name; 
} 
} 
"MynameisYoann"; 
•Valuenumber, boolean, string 
•Referencefunction, array, object

More Related Content

What's hot

ECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascriptECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascriptmatparisot
 
Clang-tidy: путешествие внутрь AST C++
Clang-tidy: путешествие внутрь AST C++Clang-tidy: путешествие внутрь AST C++
Clang-tidy: путешествие внутрь AST C++corehard_by
 
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートIIopenFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートIIAtsushi Tadokoro
 
[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30
[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30
[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30Chris Ohk
 
Функциональное реактивное программирование
Функциональное реактивное программированиеФункциональное реактивное программирование
Функциональное реактивное программированиеDmitriy Kiriyenko
 
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートIIopenFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートIIAtsushi Tadokoro
 
Corrig Projet P L S Q L
Corrig Projet  P L S Q LCorrig Projet  P L S Q L
Corrig Projet P L S Q Lbadirh
 
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングBopenFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングBAtsushi Tadokoro
 
Programación funcional en Haskell
Programación funcional en HaskellProgramación funcional en Haskell
Programación funcional en HaskellRoberto Bonvallet
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections corehard_by
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 
Hace una calculadora en jeank
Hace una calculadora en jeankHace una calculadora en jeank
Hace una calculadora en jeankHumbertoWuwu
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometrojubacalo
 
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートIIopenFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートIIAtsushi Tadokoro
 
Prof.js
Prof.jsProf.js
Prof.jsuupaa
 
Hacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual BasicHacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual BasicHumbertoWuwu
 
Java осень 2012 лекция 6
Java осень 2012 лекция 6Java осень 2012 лекция 6
Java осень 2012 лекция 6Technopark
 

What's hot (20)

ECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascriptECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
ECMAscript 2015 aka ES6 : à la découverte du nouveau javascript
 
Clang-tidy: путешествие внутрь AST C++
Clang-tidy: путешествие внутрь AST C++Clang-tidy: путешествие внутрь AST C++
Clang-tidy: путешествие внутрь AST C++
 
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートIIopenFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
openFrameworks、プログラムの制御構造の基本 - 多摩美メディアアートII
 
[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30
[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30
[C++ Korea] Effective Modern C++ Study, Item 27, 29 - 30
 
Функциональное реактивное программирование
Функциональное реактивное программированиеФункциональное реактивное программирование
Функциональное реактивное программирование
 
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートIIopenFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
openFrameworks 外部ファイルを利用する - 画像、動画 - 多摩美メディアアートII
 
Corrig Projet P L S Q L
Corrig Projet  P L S Q LCorrig Projet  P L S Q L
Corrig Projet P L S Q L
 
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングBopenFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
openFrameworks基礎 - 新規プロジェクトの作成、図形を描く 芸大グラフィクスプログラミングB
 
Programación funcional en Haskell
Programación funcional en HaskellProgramación funcional en Haskell
Programación funcional en Haskell
 
Monads
MonadsMonads
Monads
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
Hace una calculadora en jeank
Hace una calculadora en jeankHace una calculadora en jeank
Hace una calculadora en jeank
 
Java Thread Cronometro
Java Thread CronometroJava Thread Cronometro
Java Thread Cronometro
 
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートIIopenFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
openFrameworks addonを利用する ofxControlPanel ofxOpenCv - 多摩美メディアアートII
 
Cajero
CajeroCajero
Cajero
 
Prof.js
Prof.jsProf.js
Prof.js
 
Hacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual BasicHacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual Basic
 
Sbaw090630
Sbaw090630Sbaw090630
Sbaw090630
 
Java осень 2012 лекция 6
Java осень 2012 лекция 6Java осень 2012 лекция 6
Java осень 2012 лекция 6
 

Viewers also liked

OSGi, deklaratywnie
OSGi, deklaratywnieOSGi, deklaratywnie
OSGi, deklaratywnieCode-House
 
Open Source integration tools
Open Source integration toolsOpen Source integration tools
Open Source integration toolsCode-House
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSYoann Gotthilf
 
When Camel meets CDI
When Camel meets CDIWhen Camel meets CDI
When Camel meets CDICode-House
 
Web development - technologies and tools
Web development - technologies and toolsWeb development - technologies and tools
Web development - technologies and toolsYoann Gotthilf
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.jsYoann Gotthilf
 
Introduction to the MEAN stack
Introduction to the MEAN stackIntroduction to the MEAN stack
Introduction to the MEAN stackYoann Gotthilf
 
Développement Web - HTML5, CSS3, APIs Web
Développement Web - HTML5, CSS3, APIs WebDéveloppement Web - HTML5, CSS3, APIs Web
Développement Web - HTML5, CSS3, APIs WebYoann Gotthilf
 

Viewers also liked (8)

OSGi, deklaratywnie
OSGi, deklaratywnieOSGi, deklaratywnie
OSGi, deklaratywnie
 
Open Source integration tools
Open Source integration toolsOpen Source integration tools
Open Source integration tools
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
When Camel meets CDI
When Camel meets CDIWhen Camel meets CDI
When Camel meets CDI
 
Web development - technologies and tools
Web development - technologies and toolsWeb development - technologies and tools
Web development - technologies and tools
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
Introduction to the MEAN stack
Introduction to the MEAN stackIntroduction to the MEAN stack
Introduction to the MEAN stack
 
Développement Web - HTML5, CSS3, APIs Web
Développement Web - HTML5, CSS3, APIs WebDéveloppement Web - HTML5, CSS3, APIs Web
Développement Web - HTML5, CSS3, APIs Web
 

Most Common JavaScript Mistakes

  • 2. Scope «this» sample functionTimer (){ this.seconds=0; this.increment=function(){ this.seconds++; }; setInterval(function(){ this.increment(); }, 1000); } vartimer=newTimer();
  • 3. Scope «this» error functionTimer (){ this.seconds=0; this.increment=function(){ this.seconds++; }; setInterval(function(){ this.increment(); }, 1000); } vartimer=newTimer(); Uncaught TypeError: undefined is not a function
  • 4. Scope «this» solution functionTimer (){ this.seconds=0; this.increment=function(){ this.seconds++; }; varself = this; setInterval(function(){ self.increment(); }, 1000); } vartimer =newTimer();
  • 5. Bindsample varHuman =function( name ){ this.name =name; }; Human.prototype.getName=function(){ console.log( this.name ); }; varhomer =newHuman('Homer'); homer.getName(); setTimeout( function(){ homer.getName(); } , 500); setTimeout( homer.getName, 500);
  • 6. Bindoutputs varHuman =function( name ){ this.name =name; }; Human.prototype.getName=function(){ console.log( this.name ); }; varhomer =newHuman('Homer'); homer.getName(); // Homer setTimeout( function(){ homer.getName(); // Homer } , 500); setTimeout( homer.getName, 500); // Empty
  • 7. Bindsolution The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. varHuman =function( name ){ this.name =name; }; Human.prototype.getName=function(){ console.log( this.name ); }; varhomer =newHuman('Homer'); homer.getName(); // Homer setTimeout( function(){ homer.getName(); // Homer } , 500); setTimeout( homer.getName.bind(homer) , 500); // Homer
  • 8. Prototype // Shape -superclass functionShape() { this.x=0; this.y=0; } // superclass method Shape.prototype.move=function(x, y) { this.x+=x; this.y+=y; console.info('Shape moved.'); }; // Rectangle -subclass functionRectangle() { Shape.call(this); // call super constructor. } // subclass extends superclass Rectangle.prototype=Object.create(Shape.prototype); Rectangle.prototype.constructor=Rectangle; varrect=newRectangle(); console.log(rectinstanceofRectangle); console.log(rectinstanceofShape); rect.move(1, 1);
  • 9. Inheritedpropertiessample // Shape -superclass functionShape() { this.x=0; this.y=0; } // superclass method Shape.prototype.move=function(x, y) { this.x+=x; this.y+=y; console.info('Shape moved.'); }; // Rectangle -subclass functionRectangle() { Shape.call(this); // call super constructor. } // subclass extends superclass Rectangle.prototype=Object.create(Shape.prototype); Rectangle.prototype.constructor=Rectangle; Rectangle.prototype.toString=function(){ return"Rectangle"; }; varrect=newRectangle(); for(varprop inrect){ console.log(prop); } for(varprop inrect){ if(rect.hasOwnProperty(prop)){ console.log("> "+prop); } }
  • 10. Inheritedpropertiesoutputs // Shape -superclass functionShape() { this.x=0; this.y=0; } // superclass method Shape.prototype.move=function(x, y) { this.x+=x; this.y+=y; console.info('Shape moved.'); }; // Rectangle -subclass functionRectangle() { Shape.call(this); // call super constructor. } // subclass extends superclass Rectangle.prototype=Object.create(Shape.prototype); Rectangle.prototype.constructor=Rectangle; Rectangle.prototype.toString=function(){ return"Rectangle"; }; varrect=newRectangle(); for(varprop inrect){ console.log(prop); } // x, y, move, constructor, toString for(varprop inrect){ if(rect.hasOwnProperty(prop)){ console.log("> "+prop); } } // x, y,
  • 11. Comparaison sample console.log(''==0); console.log('0'==0); console.log(" trn"==0); console.log([] ==0); console.log(undefined==null); console.log({} ==0); console.log(undefined==0); if([]) { console.log('true'); } if({}) { console.log('true'); } console.log(NaN==NaN); console.log(NaN===NaN); console.log(isNaN(NaN)); console.log(''===0); console.log('0'===0); console.log({} ===0); console.log(undefined===null);
  • 12. Comparaison solution console.log(''==0); // true console.log('0'==0); // true console.log(" trn"==0); // true console.log([] ==0); // true console.log(undefined==null);// true console.log({} ==0); // false console.log(undefined==0); // false if([]) { console.log('true'); } // true if({}) { console.log('true'); }// true console.log(NaN==NaN); // false console.log(NaN===NaN); // false console.log(isNaN(NaN)); // true console.log(''===0); // false console.log('0'===0); // false console.log({} ===0); // false console.log(undefined===null);// false
  • 13. IIFE sample var x = 1; var y = 10; var m = (function(){ var x=2; y = 11; var z = 101; return 1000; })(); console.log(x); console.log(y); console.log(m); console.log(z);
  • 14. IIFE outputs var x = 1; var y = 10; var m = (function(){ var x=2; y = 11; var z = 101; return 1000; })(); console.log(x); // 1 console.log(y); // 11 console.log(m); // 1000 console.log(z); //z is not defined An immediately-invoked function expression (IIFI) is a JavaScript design pattern which produces a lexical scope using JavaScript's function scoping.
  • 15. Asynchronousloopsample for(vari=0; i<3; i++){ setTimeout(function(){ console.log(i); }); }
  • 16. Asynchronousloopproblem for(vari=0; i<3; i++){ setTimeout(function(){ console.log(i); }); } // 3, 3, 3
  • 17. Asynchronousloopsolution for(vari=0; i<3; i++){ (function(value){ setTimeout(function(){ console.log(value); }); })(i); } // 0, 1, 2
  • 18. Variables copy sample varme ={ name :'Yoann', male :true, xp:7, skills:{ java :5, js:5, android:5 }, toString:function(){ return'My name is '+this.name; } }; varclone ={}; for(varprop inme){ clone[prop] =me[prop]; } clone.xp=8; clone.male=false; clone.name ="Nexus-6"; clone.skills.dotNet=1; deleteclone.skills.android; clone.toString=function(){ return"My clone name is "+this.name; }; console.log(me); console.log(me.toString());
  • 19. Variables copy outputs { name: 'Yoann', male : true, xp: 7, skills: { java : 5, js: 5, dotNet: 1 }, toString: function(){ return 'Mynameis'+this.name; } } "MynameisYoann"; •Valuenumber, boolean, string •Referencefunction, array, object