SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Front End Workshops
II. (Not so) Basic JavaScript
concepts
Mario García Martín
mgarcia@visual-engin.com
Good practices in JavaScript
“Always code as if the guy who ends up maintaining your code will
be a violent psychopath who knows where you live.”
— John Woods
Avoid global variables
// homer.js
var isDrunk = true;
// bart.js
var isDrunk = false;
// index.html
<script src=”./homer.js”></script>
<script src=”./bart.js”></script>
console.log(isDrunk);
Global variables and functions can be overwritten by other
scripts.
Declare variables with var
Using ‘use strict’ directive will prevent undeclared variable
definitions.
function defineBeer() {
beer = 1;
}
defineBeer();
Not doing so results in global variable declaration.
'use strict';
function defineBeer() {
beer = 1;
}
defineBeer();
Uncaught ReferenceError: beer is
not defined
function defineBeer() {
var beer = 1;
}
defineBeer();
Manipulating the DOM
$('#homer).addClass('alcoholic');
// Some logic here...
$('#homer').addClass('angry');
Writing the DOM
Reading the DOM
var $homer = $('#homer');
$homer.addClass('alcoholic');
// Some logic here...
$homer.addClass('angry');
var $ul = $('#target-ul');
for (var i=0; i<4; i++) {
$ul.append('<li>' + i +
'</li>');
}
var html = '';
for (var i=0; i<4; i++) {
html += '<li>' + i + '</li>';
}
$('#target-ul').append(html);
Avoid heavy nesting
function logHomerBehaviour(homer) {
if (homer.isAtWork()) {
for (var i=0; i<4; i++) {
if (homer.isAsleep()) {
if (homer.isSnoring()) {
for (var j=0; j<2; j++) {
snore += 'Zz...';
}
console.log(snore);
}
} else {
console.log('Doughnuts!');
}
}
}
}
function logHomerBehaviour(homer) {
if (!homer.isAtWork()) { return; }
for (var i=0; i<4; i++) {
if (homer.isAsleep()) {
logHomerSleeping(homer);
} else {
console.log('Doughnuts!');
}
}
}
function logHomerSleeping(homer) {
if (!homer.isSnoring()) { return; }
console.log('Zz...Zz...');
}
Comment your code
“Good code explains itself”.
function handleEvent(ev) {
// In IE9 and earlier, use the window.event.
ev = ev || window.event;
}
Comment what you consider needed, but don’t tell others
your life story.
Comment your code
“Good code explains itself”.
function handleEvent(ev) {
// In IE9 and earlier, use the window.event.
ev = ev || window.event;
}
Comment what you consider needed, but don’t tell others
your life story.
Other good practices
Avoid eval function
Never pass a string to setTimeout or setInterval
Use === instead of ==
console.log(0 == false); // true
console.log('2' == 2); // true
console.log(0 === false); // false
console.log('2' === 2); // false
Tools: JS Lint
The only valid measurement of code quality...
WTFs/minute
More information in...
● http://www.slideshare.net/cheilmann/javascript-best-practices-1041724
● https://www.youtube.com/watch?v=hQVTIJBZook
● https://www.devbridge.com/articles/javascript-best-practices/
● http://www.codeproject.com/Articles/580165/JavaScript-Best-Practices
(Ir)Regular expressions
“Some people, when confronted with a problem, think ‘I know, I’ll
use regular expressions.’ Now they have two problems.”
— Jamie Zawinski
Creating a regular expression
var regexp1 = new RegExp('abc', 'gi');
var regexp2 = /abc/gi;
/abc/ A sequence of characters
/[abc]/ Any character from a set of characters
/[^abc]/ Any character not in a set of characters
/[0-9]/ Any character in a range of characters
/x+/ One or more occurrences of pattern x
/x*/ Zero or more occurrences
/x?/ Zero or one occurrence
/x{2,4}/ Between two and four occurrences
/(abc)/ A group
/a|b|c/ Any one of several patterns
/d/ Any digit character
/w/ An alphanumeric character [a-zA-Z0-9_]
/s/ Any whitespace character
/./ Any character except newlines
/^/ Start of input
/$/ End of input
Using a regular expression in JavaScript
/[0-9]/.test('in 1992'); //true
var neighbor = /neighbou?r/;
neighbor.test('neighbour'); // true
neighbor.test('neighbor'); // true
Through the RexExp object
Through the String object
'12345'.match(/(d)(d)+/); // ['12345', '1', '5']
'Homer drinks beer'.search(/beer/); // 13
'Ho+me[]r'.replace(/[^ws]/g, ''); // 'Homer'
'Homer drinks beer'.split(/s/); // ['Homer', 'drinks', 'beer']
var match = /d+/.exec('one two 100');
console.log(match); // ['100']
console.log(match.index); // 8
Do not abuse regular expressions...
^(?:(?:(?:0?[13578]|1[02])(/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(/|-
)(?:29|30)))(/|-)(?:[1-9]ddd|d[1-9]dd|dd[1-9]d|ddd[1-
9])$|^(?:(?:0?[1-9]|1[0-2])(/|-)(?:0?[1-9]|1d|2[0-8]))(/|-)(?:[1-
9]ddd|d[1-9]dd|dd[1-9]d|ddd[1-9])$|^(0?2(/|-)29)(/|-
)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:20)?(?:0[48]|[2468][048]|[13579][
26]))$
More information in...
● Mastering Regular Expressions, by Jeffrey E.F. Friedl
● Introducing Regular Expressions, by Michael Fitzgerald
● Regular Expressions Cookbook, by Jan Goyvaerts and Steven Levithan
● https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_E
xpressions
● http://eloquentjavascript.net/09_regexp.html
● http://www.cheatography.com/davechild/cheat-sheets/regular-
expressions/
Scopes and Closures. Prototype
“First learn computer science and all the theory. Next develop a
programming style. Then forget all that and just hack.”
— George Carrette
Lexical scope
In JavaScript, scopes are declared by functions, not by
blocks.
// Global scope
if (true) {
var x =
24;
}
console.log(x);
var x;
if (true) {
x = 24;
}
console.log(x);
Hoisting
Lexical scope
var hero = aHero();
var newSaga = function() {
var foil = aFoil();
var saga = function() {
var deed = aDeed();
console.log(hero + deed + foil);
}
}
Execution context (in-memory scope)
var hero = randStr();
var newSaga = function() {
var foil = randStr();
var saga = function() {
var deed = randStr();
console.log(hero + deed + foil);
}
saga();
saga();
}
newSaga();
newSaga();
hero = ‘gal’
newSaga = [ Function ]
foil = ‘cow’
saga = [ Function ]
deed = ‘eyes’
foil = ‘cow’
saga = [ Function ]
Execution context (in-memory scope)
var hero = randStr();
var newSaga = function() {
var foil = randStr();
var saga = function() {
var deed = randStr();
console.log(hero + deed + foil);
}
saga();
saga();
}
newSaga();
newSaga();
hero = ‘gal’
newSaga = [ Function ]
foil = ‘cow’
saga = [ Function ]
foil = ‘cow’
saga = [ Function ]
deed = ‘tips’
Execution context (in-memory scope)
var hero = randStr();
var newSaga = function() {
var foil = randStr();
var saga = function() {
var deed = randStr();
console.log(hero + deed + foil);
}
saga();
saga();
}
newSaga();
newSaga();
hero = ‘gal’
newSaga = [ Function ]
foil = ‘cow’
saga = [ Function ]
foil = ‘cat’
saga = [ Function ]
deed = ‘Rubs’
Execution context (in-memory scope)
var hero = randStr();
var newSaga = function() {
var foil = randStr();
var saga = function() {
var deed = randStr();
console.log(hero + deed + foil);
}
saga();
saga();
}
newSaga();
newSaga();
hero = ‘gal’
newSaga = [ Function ]
foil = ‘cow’
saga = [ Function ]
foil = ‘cat’
saga = [ Function ]
deed = ‘Robs’
‘this’ keyword
Invocation as a method
var homer = {
beer: 'Nice to have another one!',
info: function() {
console.log(this === homer);
console.log(this.beer);
}
};
homer.info(); // true, 'Nice to have another one!'
var bart = {
beer: 'Too young'
};
bart.info = homer.info;
bart.info(); // false, 'Too young'
function bar() {
console.log(this);
}
bar(); // global
‘this’ keyword
Invocation as a method
var anum = 0;
var foo = {
anum: 10,
baz: {
anum: 20,
bar: function() {
console.log(this.anum);
}
}
}
foo.baz.bar(); // 20
var hello = foo.baz.bar;
hello(); // 0
var foo = {
baz: function() {
console.log(this);
}
}
foo.baz(); // foo
var anotherBaz = foo.baz;
anotherBaz(); // global
‘this’ keyword
Invocation as a constructor
function Person() {
this.x = 0;
}
var person = new Person();
console.log(person.x); // 0
‘this’ keyword
Invocation with the apply and call methods
function juggle() {
var result = 0;
for (var n = 0; n < arguments.length; n++) {
result += arguments[n];
}
this.result = result;
}
var ninja1 = {};
var ninja2 = {};
juggle.apply(ninja1, [1, 2, 3, 4]); // ninja1.result = 10;
juggle.call(ninja2, 5, 6, 7, 8); // ninja2.result = 26;
Closures
Combines two things: a function, and the environment in which that function
was created.
var toast = 'Cheers!';
function makeToast() {
console.log(toast);
}
makeToast();
function
makeToast() { ... }
var toast
Closures
var scope = 'global scope';
function checkscope() {
var scope = 'local scope';
function f() { return
scope; }
return f();
}
checkscope(); // 'local scope';
var scope = 'global scope';
function checkscope() {
var scope = 'local scope';
function f() { return
scope; }
return f;
}
var func = checkscope();
func(); // 'local scope';
Closures
var fns = [];
for (var i=0; i<4; i++) {
fns.push(function() {
console.log(i);
});
}
fns[0](); // 4
fns[1](); // 4
fns[2](); // 4
fns[3](); // 4
var fns = [];
for (var i=0; i<4; i++) {
fns.push( (function(a) {
return function() {
console.log(a);
};
})(i) );
}
fns[0](); // 0
fns[1](); // 1
fns[2](); // 2
fns[3](); // 3
Constructor function
function Animal(_name) {
var name = _name;
this.getName = function() {
console.log('My name is ' + name);
};
this.setName = function(_name) {
name = _name;
};
}
var animal = new Animal('Santa');
animal.getName(); // My name is Santa
animal.setName('New Santa');
animal.getName(); // My name is New Santa
Prototype chain
var o = { a: 1 };
// o --> Object.prototype --> null
var a = ['Homer', 'Marge', 'Lisa'];
// a --> Array.prototype --> Object.prototype --> null
function f() { return 2; }
// f --> Function.prototype --> Object.prototype --> null
Prototype chain
function Animal(_name) {
// Instance properties can be set on each instance of the class
this.name = _name;
}
// Prototype properties are shared across all instances of the class.
Animal.prototype.getName = function() {
console.log('My name is ' + this.name);
};
var animal = new Animal('Santa');
animal.getName(); // My name is Santa
Inheritance
function Cat(_name) {
Animal.call(this, _name);
}
Cat.prototype = new Animal();
Cat.prototype.scratch = function() {
console.log('I love to scratch!');
}
var cat = new Cat('Claws');
cat.getName(); // My name is Claws
cat.scratch(); // I love to scratch!
More information in...
● Secrets of the JavaScript Ninja, by John Resig
● Javascript, the definitive guide, by David Flanagan
● Scope & closures, by Kyle Simpson
● http://www.sitepoint.com/demystifying-javascript-variable-scope-
hoisting/
● http://davidshariff.com/blog/what-is-the-execution-context-in-
javascript/
● http://davidshariff.com/blog/javascript-scope-chain-and-closures/
● http://davidshariff.com/blog/javascript-this-keyword/
Memory leaks
“‘Yeah, it works but you’re leaking memory everywhere. Perhaps
we should fix that”. I’ll just restart apache every 10 requests.”
— Rasmus Lerdorf
Circular references
<div id="target">Element</div>
<script type="text/javascript">
var obj = document.getElementById('target');
document.getElementById('target').property = obj;
obj.bigString = new Array(10000).join('*');
</script>
Example 1
Circular references
<div id="target"></div>
<script type="text/javascript">
function myFunction(element) {
this.elementReference = element;
element.property = this;
}
function leak() {
new myFunction(document.getElementById('target'));
}
leak();
</script>
Example 2
Closures and circular references
<button id="element">Click Me</button>
<script type="text/javascript">
function outerFunction() {
var obj = document.getElementById('element');
obj.onclick = function innerFunction() {
console.log('Hi! I will leak');
};
obj.bigString = new Array(10000).join('*');
};
outerFunction();
</script>
Closures. Break the circular reference
<button id="element">Click Me</button>
<script type="text/javascript">
function outerFunction() {
var obj = document.getElementById('element');
obj.onclick = function innerFunction() {
console.log('Hi! I have avoided the leak');
};
obj.bigString = new Array(10000).join('*'));
obj = null; //This breaks the circular reference
};
outerFunction();
</script>
Closures. Add another closure
<button id="element">Click Me</button>
<script type="text/javascript">
function outerFunction() {
var anotherObj = function innerFunction() {
console.log('Hi! I have avoided the leak');
};
(function anotherInnerFunction() {
var obj = document.getElementById('element');
obj.onclick = anotherObj;
})();
};
outerFunction();
</script>
Closures. Avoid the initial closure
<button id="element">Click Me</button>
<script type="text/javascript">
function outerFunction() {
var obj = document.getElementById('element');
obj.onclick = doesNotLeak;
}
function doesNotLeak() {
console.log('Hi! I have avoided the leak');
}
outerFunction();
</script>
More information in...
● https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Memory_Management
● http://www.ibm.com/developerworks/web/library/wa-memleak/
● https://msdn.microsoft.com/en-us/magazine/ff728624.aspx
Thanks for your attention!
Leave your questions on the comments section
Workshop 1: Good practices in JavaScript

Weitere ähnliche Inhalte

Was ist angesagt?

AngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile ServicesAngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile ServicesRainer Stropek
 
Testing Backbone applications with Jasmine
Testing Backbone applications with JasmineTesting Backbone applications with Jasmine
Testing Backbone applications with JasmineLeon van der Grient
 
AngularJS $Provide Service
AngularJS $Provide ServiceAngularJS $Provide Service
AngularJS $Provide ServiceEyal Vardi
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsPiotr Pelczar
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0Eyal Vardi
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 ArchitectureEyal Vardi
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptjnewmanux
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS ArchitectureEyal Vardi
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Eyal Vardi
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Konstantin Kudryashov
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRaimonds Simanovskis
 
Jasmine - why JS tests don't smell fishy
Jasmine - why JS tests don't smell fishyJasmine - why JS tests don't smell fishy
Jasmine - why JS tests don't smell fishyIgor Napierala
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaExoLeaders.com
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with JasmineTim Tyrrell
 

Was ist angesagt? (20)

AngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile ServicesAngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile Services
 
Testing Backbone applications with Jasmine
Testing Backbone applications with JasmineTesting Backbone applications with Jasmine
Testing Backbone applications with Jasmine
 
Map kit light
Map kit lightMap kit light
Map kit light
 
AngularJS $Provide Service
AngularJS $Provide ServiceAngularJS $Provide Service
AngularJS $Provide Service
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Frontin like-a-backer
Frontin like-a-backerFrontin like-a-backer
Frontin like-a-backer
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
 
Jasmine - why JS tests don't smell fishy
Jasmine - why JS tests don't smell fishyJasmine - why JS tests don't smell fishy
Jasmine - why JS tests don't smell fishy
 
AngularJs
AngularJsAngularJs
AngularJs
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 

Andere mochten auch

Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsVisual Engineering
 
Javascript cheat-sheet-v1
Javascript cheat-sheet-v1Javascript cheat-sheet-v1
Javascript cheat-sheet-v1hccit
 
Javascript The Good Parts v2
Javascript The Good Parts v2Javascript The Good Parts v2
Javascript The Good Parts v2Federico Galassi
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Visual Engineering
 

Andere mochten auch (6)

Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build tools
 
Javascript cheat-sheet-v1
Javascript cheat-sheet-v1Javascript cheat-sheet-v1
Javascript cheat-sheet-v1
 
Javascript The Good Parts v2
Javascript The Good Parts v2Javascript The Good Parts v2
Javascript The Good Parts v2
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 

Ähnlich wie Workshop 1: Good practices in JavaScript

LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesTobias Oetiker
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesAnkit Rastogi
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScriptJohannes Hoppe
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScriptJohannes Hoppe
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gearsdion
 
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays
 
¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?jaespinmora
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Tsuyoshi Yamamoto
 
JavaScript Proven Practises
JavaScript Proven PractisesJavaScript Proven Practises
JavaScript Proven PractisesRobert MacLean
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5arajivmordani
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Wilson Su
 

Ähnlich wie Workshop 1: Good practices in JavaScript (20)

Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial Slides
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gears
 
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
 
¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?
 
YUI 3
YUI 3YUI 3
YUI 3
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
 
JavaScript Proven Practises
JavaScript Proven PractisesJavaScript Proven Practises
JavaScript Proven Practises
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 

Mehr von Visual Engineering

Workshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSWorkshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSVisual Engineering
 
Workshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operatorsWorkshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operatorsVisual Engineering
 
Workshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensionesWorkshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensionesVisual Engineering
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresVisual Engineering
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftVisual Engineering
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideVisual Engineering
 
Workshop 25: React Native - Components
Workshop 25: React Native - ComponentsWorkshop 25: React Native - Components
Workshop 25: React Native - ComponentsVisual Engineering
 
Workshop 24: React Native Introduction
Workshop 24: React Native IntroductionWorkshop 24: React Native Introduction
Workshop 24: React Native IntroductionVisual Engineering
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedVisual Engineering
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareVisual Engineering
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxVisual Engineering
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionVisual Engineering
 
Workshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsWorkshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsVisual Engineering
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIVisual Engineering
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIVisual Engineering
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSVisual Engineering
 

Mehr von Visual Engineering (20)

Workshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSWorkshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJS
 
Workshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operatorsWorkshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operators
 
Workshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensionesWorkshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensiones
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - Structures
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Workshop 25: React Native - Components
Workshop 25: React Native - ComponentsWorkshop 25: React Native - Components
Workshop 25: React Native - Components
 
Workshop 24: React Native Introduction
Workshop 24: React Native IntroductionWorkshop 24: React Native Introduction
Workshop 24: React Native Introduction
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Workshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsWorkshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effects
 
Workshop 17: EmberJS parte II
Workshop 17: EmberJS parte IIWorkshop 17: EmberJS parte II
Workshop 17: EmberJS parte II
 
Workshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte IWorkshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte I
 
Workshop 15: Ionic framework
Workshop 15: Ionic frameworkWorkshop 15: Ionic framework
Workshop 15: Ionic framework
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 

Kürzlich hochgeladen

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 

Kürzlich hochgeladen (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 

Workshop 1: Good practices in JavaScript

  • 1. Front End Workshops II. (Not so) Basic JavaScript concepts Mario García Martín mgarcia@visual-engin.com
  • 2. Good practices in JavaScript “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” — John Woods
  • 3. Avoid global variables // homer.js var isDrunk = true; // bart.js var isDrunk = false; // index.html <script src=”./homer.js”></script> <script src=”./bart.js”></script> console.log(isDrunk); Global variables and functions can be overwritten by other scripts.
  • 4. Declare variables with var Using ‘use strict’ directive will prevent undeclared variable definitions. function defineBeer() { beer = 1; } defineBeer(); Not doing so results in global variable declaration. 'use strict'; function defineBeer() { beer = 1; } defineBeer(); Uncaught ReferenceError: beer is not defined function defineBeer() { var beer = 1; } defineBeer();
  • 5. Manipulating the DOM $('#homer).addClass('alcoholic'); // Some logic here... $('#homer').addClass('angry'); Writing the DOM Reading the DOM var $homer = $('#homer'); $homer.addClass('alcoholic'); // Some logic here... $homer.addClass('angry'); var $ul = $('#target-ul'); for (var i=0; i<4; i++) { $ul.append('<li>' + i + '</li>'); } var html = ''; for (var i=0; i<4; i++) { html += '<li>' + i + '</li>'; } $('#target-ul').append(html);
  • 6. Avoid heavy nesting function logHomerBehaviour(homer) { if (homer.isAtWork()) { for (var i=0; i<4; i++) { if (homer.isAsleep()) { if (homer.isSnoring()) { for (var j=0; j<2; j++) { snore += 'Zz...'; } console.log(snore); } } else { console.log('Doughnuts!'); } } } } function logHomerBehaviour(homer) { if (!homer.isAtWork()) { return; } for (var i=0; i<4; i++) { if (homer.isAsleep()) { logHomerSleeping(homer); } else { console.log('Doughnuts!'); } } } function logHomerSleeping(homer) { if (!homer.isSnoring()) { return; } console.log('Zz...Zz...'); }
  • 7. Comment your code “Good code explains itself”. function handleEvent(ev) { // In IE9 and earlier, use the window.event. ev = ev || window.event; } Comment what you consider needed, but don’t tell others your life story.
  • 8. Comment your code “Good code explains itself”. function handleEvent(ev) { // In IE9 and earlier, use the window.event. ev = ev || window.event; } Comment what you consider needed, but don’t tell others your life story.
  • 9. Other good practices Avoid eval function Never pass a string to setTimeout or setInterval Use === instead of == console.log(0 == false); // true console.log('2' == 2); // true console.log(0 === false); // false console.log('2' === 2); // false Tools: JS Lint
  • 10. The only valid measurement of code quality... WTFs/minute
  • 11. More information in... ● http://www.slideshare.net/cheilmann/javascript-best-practices-1041724 ● https://www.youtube.com/watch?v=hQVTIJBZook ● https://www.devbridge.com/articles/javascript-best-practices/ ● http://www.codeproject.com/Articles/580165/JavaScript-Best-Practices
  • 12. (Ir)Regular expressions “Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.” — Jamie Zawinski
  • 13. Creating a regular expression var regexp1 = new RegExp('abc', 'gi'); var regexp2 = /abc/gi; /abc/ A sequence of characters /[abc]/ Any character from a set of characters /[^abc]/ Any character not in a set of characters /[0-9]/ Any character in a range of characters /x+/ One or more occurrences of pattern x /x*/ Zero or more occurrences /x?/ Zero or one occurrence /x{2,4}/ Between two and four occurrences /(abc)/ A group /a|b|c/ Any one of several patterns /d/ Any digit character /w/ An alphanumeric character [a-zA-Z0-9_] /s/ Any whitespace character /./ Any character except newlines /^/ Start of input /$/ End of input
  • 14. Using a regular expression in JavaScript /[0-9]/.test('in 1992'); //true var neighbor = /neighbou?r/; neighbor.test('neighbour'); // true neighbor.test('neighbor'); // true Through the RexExp object Through the String object '12345'.match(/(d)(d)+/); // ['12345', '1', '5'] 'Homer drinks beer'.search(/beer/); // 13 'Ho+me[]r'.replace(/[^ws]/g, ''); // 'Homer' 'Homer drinks beer'.split(/s/); // ['Homer', 'drinks', 'beer'] var match = /d+/.exec('one two 100'); console.log(match); // ['100'] console.log(match.index); // 8
  • 15. Do not abuse regular expressions... ^(?:(?:(?:0?[13578]|1[02])(/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(/|- )(?:29|30)))(/|-)(?:[1-9]ddd|d[1-9]dd|dd[1-9]d|ddd[1- 9])$|^(?:(?:0?[1-9]|1[0-2])(/|-)(?:0?[1-9]|1d|2[0-8]))(/|-)(?:[1- 9]ddd|d[1-9]dd|dd[1-9]d|ddd[1-9])$|^(0?2(/|-)29)(/|- )(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:20)?(?:0[48]|[2468][048]|[13579][ 26]))$
  • 16. More information in... ● Mastering Regular Expressions, by Jeffrey E.F. Friedl ● Introducing Regular Expressions, by Michael Fitzgerald ● Regular Expressions Cookbook, by Jan Goyvaerts and Steven Levithan ● https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_E xpressions ● http://eloquentjavascript.net/09_regexp.html ● http://www.cheatography.com/davechild/cheat-sheets/regular- expressions/
  • 17. Scopes and Closures. Prototype “First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack.” — George Carrette
  • 18. Lexical scope In JavaScript, scopes are declared by functions, not by blocks. // Global scope if (true) { var x = 24; } console.log(x); var x; if (true) { x = 24; } console.log(x); Hoisting
  • 19. Lexical scope var hero = aHero(); var newSaga = function() { var foil = aFoil(); var saga = function() { var deed = aDeed(); console.log(hero + deed + foil); } }
  • 20. Execution context (in-memory scope) var hero = randStr(); var newSaga = function() { var foil = randStr(); var saga = function() { var deed = randStr(); console.log(hero + deed + foil); } saga(); saga(); } newSaga(); newSaga(); hero = ‘gal’ newSaga = [ Function ] foil = ‘cow’ saga = [ Function ] deed = ‘eyes’ foil = ‘cow’ saga = [ Function ]
  • 21. Execution context (in-memory scope) var hero = randStr(); var newSaga = function() { var foil = randStr(); var saga = function() { var deed = randStr(); console.log(hero + deed + foil); } saga(); saga(); } newSaga(); newSaga(); hero = ‘gal’ newSaga = [ Function ] foil = ‘cow’ saga = [ Function ] foil = ‘cow’ saga = [ Function ] deed = ‘tips’
  • 22. Execution context (in-memory scope) var hero = randStr(); var newSaga = function() { var foil = randStr(); var saga = function() { var deed = randStr(); console.log(hero + deed + foil); } saga(); saga(); } newSaga(); newSaga(); hero = ‘gal’ newSaga = [ Function ] foil = ‘cow’ saga = [ Function ] foil = ‘cat’ saga = [ Function ] deed = ‘Rubs’
  • 23. Execution context (in-memory scope) var hero = randStr(); var newSaga = function() { var foil = randStr(); var saga = function() { var deed = randStr(); console.log(hero + deed + foil); } saga(); saga(); } newSaga(); newSaga(); hero = ‘gal’ newSaga = [ Function ] foil = ‘cow’ saga = [ Function ] foil = ‘cat’ saga = [ Function ] deed = ‘Robs’
  • 24. ‘this’ keyword Invocation as a method var homer = { beer: 'Nice to have another one!', info: function() { console.log(this === homer); console.log(this.beer); } }; homer.info(); // true, 'Nice to have another one!' var bart = { beer: 'Too young' }; bart.info = homer.info; bart.info(); // false, 'Too young' function bar() { console.log(this); } bar(); // global
  • 25. ‘this’ keyword Invocation as a method var anum = 0; var foo = { anum: 10, baz: { anum: 20, bar: function() { console.log(this.anum); } } } foo.baz.bar(); // 20 var hello = foo.baz.bar; hello(); // 0 var foo = { baz: function() { console.log(this); } } foo.baz(); // foo var anotherBaz = foo.baz; anotherBaz(); // global
  • 26. ‘this’ keyword Invocation as a constructor function Person() { this.x = 0; } var person = new Person(); console.log(person.x); // 0
  • 27. ‘this’ keyword Invocation with the apply and call methods function juggle() { var result = 0; for (var n = 0; n < arguments.length; n++) { result += arguments[n]; } this.result = result; } var ninja1 = {}; var ninja2 = {}; juggle.apply(ninja1, [1, 2, 3, 4]); // ninja1.result = 10; juggle.call(ninja2, 5, 6, 7, 8); // ninja2.result = 26;
  • 28. Closures Combines two things: a function, and the environment in which that function was created. var toast = 'Cheers!'; function makeToast() { console.log(toast); } makeToast(); function makeToast() { ... } var toast
  • 29. Closures var scope = 'global scope'; function checkscope() { var scope = 'local scope'; function f() { return scope; } return f(); } checkscope(); // 'local scope'; var scope = 'global scope'; function checkscope() { var scope = 'local scope'; function f() { return scope; } return f; } var func = checkscope(); func(); // 'local scope';
  • 30. Closures var fns = []; for (var i=0; i<4; i++) { fns.push(function() { console.log(i); }); } fns[0](); // 4 fns[1](); // 4 fns[2](); // 4 fns[3](); // 4 var fns = []; for (var i=0; i<4; i++) { fns.push( (function(a) { return function() { console.log(a); }; })(i) ); } fns[0](); // 0 fns[1](); // 1 fns[2](); // 2 fns[3](); // 3
  • 31. Constructor function function Animal(_name) { var name = _name; this.getName = function() { console.log('My name is ' + name); }; this.setName = function(_name) { name = _name; }; } var animal = new Animal('Santa'); animal.getName(); // My name is Santa animal.setName('New Santa'); animal.getName(); // My name is New Santa
  • 32. Prototype chain var o = { a: 1 }; // o --> Object.prototype --> null var a = ['Homer', 'Marge', 'Lisa']; // a --> Array.prototype --> Object.prototype --> null function f() { return 2; } // f --> Function.prototype --> Object.prototype --> null
  • 33. Prototype chain function Animal(_name) { // Instance properties can be set on each instance of the class this.name = _name; } // Prototype properties are shared across all instances of the class. Animal.prototype.getName = function() { console.log('My name is ' + this.name); }; var animal = new Animal('Santa'); animal.getName(); // My name is Santa
  • 34. Inheritance function Cat(_name) { Animal.call(this, _name); } Cat.prototype = new Animal(); Cat.prototype.scratch = function() { console.log('I love to scratch!'); } var cat = new Cat('Claws'); cat.getName(); // My name is Claws cat.scratch(); // I love to scratch!
  • 35. More information in... ● Secrets of the JavaScript Ninja, by John Resig ● Javascript, the definitive guide, by David Flanagan ● Scope & closures, by Kyle Simpson ● http://www.sitepoint.com/demystifying-javascript-variable-scope- hoisting/ ● http://davidshariff.com/blog/what-is-the-execution-context-in- javascript/ ● http://davidshariff.com/blog/javascript-scope-chain-and-closures/ ● http://davidshariff.com/blog/javascript-this-keyword/
  • 36. Memory leaks “‘Yeah, it works but you’re leaking memory everywhere. Perhaps we should fix that”. I’ll just restart apache every 10 requests.” — Rasmus Lerdorf
  • 37. Circular references <div id="target">Element</div> <script type="text/javascript"> var obj = document.getElementById('target'); document.getElementById('target').property = obj; obj.bigString = new Array(10000).join('*'); </script> Example 1
  • 38. Circular references <div id="target"></div> <script type="text/javascript"> function myFunction(element) { this.elementReference = element; element.property = this; } function leak() { new myFunction(document.getElementById('target')); } leak(); </script> Example 2
  • 39. Closures and circular references <button id="element">Click Me</button> <script type="text/javascript"> function outerFunction() { var obj = document.getElementById('element'); obj.onclick = function innerFunction() { console.log('Hi! I will leak'); }; obj.bigString = new Array(10000).join('*'); }; outerFunction(); </script>
  • 40. Closures. Break the circular reference <button id="element">Click Me</button> <script type="text/javascript"> function outerFunction() { var obj = document.getElementById('element'); obj.onclick = function innerFunction() { console.log('Hi! I have avoided the leak'); }; obj.bigString = new Array(10000).join('*')); obj = null; //This breaks the circular reference }; outerFunction(); </script>
  • 41. Closures. Add another closure <button id="element">Click Me</button> <script type="text/javascript"> function outerFunction() { var anotherObj = function innerFunction() { console.log('Hi! I have avoided the leak'); }; (function anotherInnerFunction() { var obj = document.getElementById('element'); obj.onclick = anotherObj; })(); }; outerFunction(); </script>
  • 42. Closures. Avoid the initial closure <button id="element">Click Me</button> <script type="text/javascript"> function outerFunction() { var obj = document.getElementById('element'); obj.onclick = doesNotLeak; } function doesNotLeak() { console.log('Hi! I have avoided the leak'); } outerFunction(); </script>
  • 43. More information in... ● https://developer.mozilla.org/en- US/docs/Web/JavaScript/Memory_Management ● http://www.ibm.com/developerworks/web/library/wa-memleak/ ● https://msdn.microsoft.com/en-us/magazine/ff728624.aspx
  • 44. Thanks for your attention! Leave your questions on the comments section