javascript survival guide
Post on 23-Sep-2014
31 views
DESCRIPTION
How to stop to worry and starting to love JavaScriptTRANSCRIPT
![Page 1: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/1.jpg)
Giordano Scalzo
JavaScript Survival Guide
Roma,5 marzo 2011
![Page 2: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/2.jpg)
I’m not a guru!
![Page 3: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/3.jpg)
I’m still learning
![Page 4: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/4.jpg)
Java/C++ guy in the past
![Page 5: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/5.jpg)
Ruby/ObjectiveC guy in the present
![Page 6: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/6.jpg)
Why to learn JavaScript?
![Page 7: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/7.jpg)
JavaScript isn’t this anymore
![Page 8: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/8.jpg)
JavaScript is everywhere!
![Page 9: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/9.jpg)
JavaScript is popular!
![Page 10: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/10.jpg)
Technology Radar
JavaScript is trendy!
![Page 11: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/11.jpg)
Technology Radar
JavaScript is trendy!
![Page 12: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/12.jpg)
It should be easy...
![Page 13: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/13.jpg)
Hacked by Brendan Eich in one week...
![Page 14: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/14.jpg)
At the beginning...
![Page 15: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/15.jpg)
after a while...
![Page 16: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/16.jpg)
traps everywhere..
![Page 17: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/17.jpg)
traps everywhere..
![Page 18: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/18.jpg)
and...
![Page 19: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/19.jpg)
:-(
![Page 20: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/20.jpg)
and so...
![Page 21: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/21.jpg)
![Page 22: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/22.jpg)
Back to study!
![Page 23: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/23.jpg)
Started a notebook...
![Page 24: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/24.jpg)
Essential
Scope
![Page 25: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/25.jpg)
function sum(x, y){ // implied global result = x + y; return result;}
{antipattern}
![Page 26: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/26.jpg)
Global variables are evil!
![Page 27: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/27.jpg)
Variables clash
![Page 28: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/28.jpg)
Always declare variables with var
function sum(x, y){ var result = x + y; return result;}
{pattern}
![Page 29: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/29.jpg)
function foo(){ var a = b = 0; //...}
{antipattern}
![Page 30: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/30.jpg)
function foo(){ var a = (b = 0); //...}
{antipattern}
b become global
![Page 31: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/31.jpg)
function foo(){ var a, b; a = b = 0; //...}
don’t use assign chain in definition
{pattern}
![Page 32: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/32.jpg)
Hoisting
myname = "global"; // global variable function func(){ // code... console.log(myname); // "undefined"
// code... var myname = "local"; console.log(myname); // "local"}
func();
{antipattern}
![Page 33: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/33.jpg)
Hoisting
myname = "global"; // global variable function func(){ // code... console.log(myname); // "undefined"
// code... var myname = "local"; console.log(myname); // "local"}
func();
{antipattern}
![Page 34: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/34.jpg)
myname = "global"; // global variable function func(){ // code... console.log(myname); // "undefined"
// code... var myname = "local"; console.log(myname); // "local"}
func();
Hoisting
{antipattern}
![Page 35: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/35.jpg)
Hoisting
myname = "global"; // global variable function func(){ var myname = "declared"; // code... console.log(myname); // "declared"
// code... myname = "local"; console.log(myname); // "local"}
func();
{pattern}
![Page 36: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/36.jpg)
“Variables should be declared as close to their usage as possible”
Robert C. Martin - Clean Code
Against minimum vertical distance principle
![Page 37: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/37.jpg)
function func(){ var a = 1,
b = 2, sum = a + b, myobject = {}, i, j;
// function body...}
{pattern}
Single var pattern
![Page 38: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/38.jpg)
Don’t forget comma otherwise...
![Page 39: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/39.jpg)
... variables become globals
![Page 40: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/40.jpg)
EssentialLiteral and Constructor
![Page 41: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/41.jpg)
In JavaScript almost everything is an object
![Page 42: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/42.jpg)
var person = new Object();person.name = "Scott";person.say = function(){ return "I am " + this.name;};
console.log(person.say());
It’s easy...
![Page 43: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/43.jpg)
var person = new Object();person.name = "Scott";person.say = function(){ return "I am " + this.name;};
console.log(person.say());
{antipattern}
but wrong! :-(
![Page 44: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/44.jpg)
var person = {};person.name = "Scott";person.say = function(){ return "I am " + this.name;};
console.log(person.say());
{pattern}
![Page 45: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/45.jpg)
What if we need similar objects...
var person = {};person.name = "Scott";person.say = function(){ return "I am " + this.name;};console.log(person.say()); // I am Scott
var otherPerson = {};otherPerson.name = "Tiger";otherPerson.say = function(){ return "I am " + this.name;};
console.log(otherPerson.say()); // I am Tiger
![Page 46: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/46.jpg)
A lot of duplication
var person = {};person.name = "Scott";person.say = function(){ return "I am " + this.name;};console.log(person.say()); // I am Scott
var otherPerson = {};otherPerson.name = "Tiger";otherPerson.say = function(){ return "I am " + this.name;};
console.log(otherPerson.say()); // I am Tiger
![Page 47: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/47.jpg)
Duplication is evil!
![Page 48: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/48.jpg)
var Person = function(name){ this.name = name; this.say = function(){ return "I am " + this.name; }}var person = new Person("Scott");
console.log(person.say()); // I am Scott
Custom Constructor Functions
{pattern}
![Page 49: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/49.jpg)
Behind the scenes...
var Person = function(name){ // var this = {}; // this.prototype = {constructor: this} this.name = name; this.say = function(){ return "I am " + this.name; }; // return this;};
{pattern}
![Page 50: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/50.jpg)
var Person = function(name){ this.name = name; this.say = function(){ return "I am " + this.name; };};
var scott = new Person('Scott');var tiger = new Person('Tiger');
console.log(scott.say());console.log(tiger.say());
So, at the end...
{pattern}
![Page 51: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/51.jpg)
What if we forget new?
![Page 52: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/52.jpg)
var Person = function(name){ this.name = name; this.say = function(){ return "I am " + this.name; };};
var scott = new Person('Scott')var adam = Person('Adam')
console.log(typeof scott); //objectconsole.log(scott.name); // Scottconsole.log(typeof adam); //'undefined'console.log(window.name); // Adam
this will point to global object
![Page 53: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/53.jpg)
var Person = function(name){ this.name = name; this.say = function(){ return "I am " + this.name; };};
var scott = new Person('Scott')var adam = Person('Adam')
console.log(typeof scott); //objectconsole.log(scott.name); // Scottconsole.log(typeof adam); //'undefined'console.log(window.name); // Adam
this will point to global object
![Page 54: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/54.jpg)
Enforce new pattern one: naming convention
![Page 55: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/55.jpg)
var Person = function(name){ var that = {}; that.name = name; that.say = function(){ return "I am " + that.name;}; return that;};
var scott = new Person('Scott')var adam = Person('Adam')
console.log(typeof scott); //Objectconsole.log(scott.name); // Scott
console.log(typeof adam); //Objectconsole.log(adam.name); // Adam
{pattern}
![Page 56: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/56.jpg)
var Person = function(name){ var that = {}; that.name = name; that.say = function(){ return "I am " + that.name;}; return that;};
var scott = new Person('Scott')var adam = Person('Adam')
console.log(typeof scott); //Objectconsole.log(scott.name); // Scott
console.log(typeof adam); //Objectconsole.log(adam.name); // Adam
{pattern}
![Page 57: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/57.jpg)
Drawback: we loose prototype reference :-(
var Person = function(name){ var that = {}; that.name = name; that.say = function(){ return "I am " + that.name; }; return that;};
Person.prototype.iamhumanbeing = true;
var scott = new Person('Scott')var adam = Person('Adam')
console.log(scott.iamhumanbeing); // undefinedconsole.log(adam.iamhumanbeing); // undefined
![Page 58: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/58.jpg)
Drawback: we loose prototype reference :-(
var Person = function(name){ var that = {}; that.name = name; that.say = function(){ return "I am " + that.name; }; return that;};
Person.prototype.iamhumanbeing = true;
var scott = new Person('Scott')var adam = Person('Adam')
console.log(scott.iamhumanbeing); // undefinedconsole.log(adam.iamhumanbeing); // undefined
![Page 59: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/59.jpg)
Interm!zoPrototype property
![Page 60: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/60.jpg)
Define ancestors chain
var foo = {one: 1, two: 2};var bar = {three: 3};foo.__proto__ = bar;console.log(foo.one);console.log(foo.three);
![Page 61: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/61.jpg)
Define ancestors chain
var foo = {one: 1, two: 2};var bar = {three: 3};foo.__proto__ = bar;console.log(foo.one);console.log(foo.three);
![Page 62: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/62.jpg)
foo
one: 1
__proto__
bar
three: 3
two: 2
console.log(foo.one);
![Page 63: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/63.jpg)
foo
one: 1
__proto__
bar
three: 3
two: 2
console.log(foo.one);
![Page 64: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/64.jpg)
foo
one: 1
__proto__
bar
three: 3
two: 2
console.log(foo.three);
![Page 65: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/65.jpg)
foo
one: 1
__proto__
bar
three: 3
two: 2
console.log(foo.three);
![Page 66: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/66.jpg)
foo
one: 1
__proto__
bar
three: 3
two: 2
console.log(foo.three);
![Page 67: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/67.jpg)
Interm!zoEnd
![Page 68: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/68.jpg)
var Person = function(name){ // this.prototype = {constructor: this} var that = {}; that.name = name; that.say = function(){ return "I am " + that.name; }; return that;};
Behind the scenes...
![Page 69: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/69.jpg)
var Person = function(name){ if (this instanceof Person) { this.name = name; this.say = function(){ return "I am " + that.name; } } else { return new Person(name); }};Person.prototype.iamhumanbeing = true;var scott = new Person('Scott')var adam = Person('Adam')console.log(scott.name); // Scottconsole.log(adam.name); // Adamconsole.log(scott.iamhumanbeing); // trueconsole.log(adam.iamhumanbeing); // true
Self invoking constructor
{pattern}
![Page 70: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/70.jpg)
var Person = function(name){ if (this instanceof Person) { this.name = name; this.say = function(){ return "I am " + that.name; } } else { return new Person(name); }};Person.prototype.iamhumanbeing = true;var scott = new Person('Scott')var adam = Person('Adam')console.log(scott.name); // Scottconsole.log(adam.name); // Adamconsole.log(scott.iamhumanbeing); // trueconsole.log(adam.iamhumanbeing); // true
Self invoking constructor
{pattern}
![Page 71: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/71.jpg)
var Person = function(name){ if (this instanceof Person) { this.name = name; this.say = function(){ return "I am " + that.name; } } else { return new Person(name); }};Person.prototype.iamhumanbeing = true;var scott = new Person('Scott')var adam = Person('Adam')console.log(scott.name); // Scottconsole.log(adam.name); // Adamconsole.log(scott.iamhumanbeing); // trueconsole.log(adam.iamhumanbeing); // true
Self invoking constructor
{pattern}
![Page 72: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/72.jpg)
var Person = function(name){ if (this instanceof Person) { this.name = name; this.say = function(){ return "I am " + that.name; } } else { return new Person(name); }};Person.prototype.iamhumanbeing = true;var scott = new Person('Scott')var adam = Person('Adam')console.log(scott.name); // Scottconsole.log(adam.name); // Adamconsole.log(scott.iamhumanbeing); // trueconsole.log(adam.iamhumanbeing); // true
Self invoking constructor
{pattern}
![Page 73: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/73.jpg)
EssentialFunctions
![Page 74: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/74.jpg)
Functions as first class objects
![Page 75: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/75.jpg)
Immediate functions
(function(){ alert('watch out!');})();
![Page 76: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/76.jpg)
Initialization pattern
(function(){ var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], today = new Date(), msg = 'Today is ' + days[today.getDay()] + ', ' + today.getDate(); console.log(msg);})(); // "Today is Wed, 10"
{pattern}
![Page 77: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/77.jpg)
Function scope
![Page 78: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/78.jpg)
// constructors function Parent(){}
function Child(){}
// a variablevar some_var = 1;// some objects var module1 = {};module1.data = { a: 1, b: 2};var module2 = {};
{antipattern}
5 globals...
![Page 79: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/79.jpg)
// global object var MYAPP = (function(){ var my = {}; // constructors my.Parent = function(){}; my.Child = function(){}; // a variable my.some_var = 1; // an object container my.modules = {}; // nested objects my.modules.module1 = {}; my.modules.module1.data = { a: 1, b: 2 }; my.modules.module2 = {}; return my;})();
console.log(MYAPP.modules.module1.data.a); // 1
{pattern}1 global!
![Page 80: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/80.jpg)
// global object var MYAPP = (function(){ var my = {}; // constructors my.Parent = function(){}; my.Child = function(){}; // a variable my.some_var = 1; // an object container my.modules = {}; // nested objects my.modules.module1 = {}; my.modules.module1.data = { a: 1, b: 2 }; my.modules.module2 = {}; return my;})();
console.log(MYAPP.modules.module1.data.a); // 1
{pattern}1 global!
![Page 81: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/81.jpg)
What about encapsulation?
![Page 82: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/82.jpg)
function Gadget(){ this.name = 'iPod'; this.stretch = function(){ return 'iPad'; }};
var toy = new Gadget();console.log(toy.name); // `iPod` toy.name = 'Zune'console.log(toy.name); // `Zune` is public console.log(toy.stretch()); // stretch() is public
{antipattern}
![Page 83: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/83.jpg)
function Gadget(){ var name = 'iPod'; this.getName = function(){ return name; }};
var toy = new Gadget();console.log(toy.getName()); // `iPod`toy.name = 'Zune'console.log(toy.getName()); // `iPod`
Create private member
{pattern}
![Page 84: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/84.jpg)
function Gadget(){ var name = 'iPod'; this.getName = function(){ return name; }};
var toy = new Gadget();console.log(toy.getName()); // `iPod`toy.name = 'Zune'console.log(toy.getName()); // `iPod`
Create private member
{pattern}
![Page 85: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/85.jpg)
function Gadget(){ var name = 'iPod'; this.getName = function(){ return name; }};
var toy = new Gadget();console.log(toy.getName()); // `iPod`toy.name = 'Zune'console.log(toy.getName()); // `iPod`
Create private member
{pattern}
![Page 86: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/86.jpg)
function Gadget(){ var name = 'iPod'; this.getName = function(){ return name; }};
var toy = new Gadget();console.log(toy.getName()); // `iPod`toy.name = 'Zune'console.log(toy.getName()); // `iPod`
Create private member
{pattern}
![Page 87: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/87.jpg)
function Gadget() { var name = 'iPod'; var upgrade = function(){ return 'iPhone'; }
this.getName = function () { return name; } this.pay = function() { return upgrade(); }};
var toy = new Gadget(); console.log(toy.pay()); // `iPhone`console.log(toy.upgrade()); // `error`
{pattern}
for methods too
![Page 88: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/88.jpg)
AdvancedTest Framework
![Page 89: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/89.jpg)
JavaScript is madly in love with Testing.
![Page 90: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/90.jpg)
JavaScript is madly in love with Testing.
JSUnit
![Page 91: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/91.jpg)
JavaScript is madly in love with Testing.
JSUnit
JSTest
![Page 92: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/92.jpg)
JavaScript is madly in love with Testing.
QUnit JSUnit
JSTest
![Page 93: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/93.jpg)
JavaScript is madly in love with Testing.
QUnit JSUnit
JSTest Vows
![Page 94: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/94.jpg)
JavaScript is madly in love with Testing.
QUnit JSUnit
JSTestjsUnitTest Vows
![Page 95: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/95.jpg)
JavaScript is madly in love with Testing.
Blue Ridge
QUnit JSUnit
JSTestjsUnitTest Vows
![Page 96: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/96.jpg)
Screw unit
JavaScript is madly in love with Testing.
Blue Ridge
QUnit JSUnit
JSTestjsUnitTest Vows
![Page 97: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/97.jpg)
Screw unit
JavaScript is madly in love with Testing.
Blue Ridge
QUnit JSUnit
JSTest
YUI Test
jsUnitTest Vows
![Page 98: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/98.jpg)
Screw unit
JavaScript is madly in love with Testing.
Blue Ridge
JSpecQUnit JSUnit
JSTest
YUI Test
jsUnitTest Vows
![Page 99: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/99.jpg)
Screw unit
JavaScript is madly in love with Testing.
Blue Ridge
JSpecQUnit JSUnit
JSTest
UnitestingYUI Test
jsUnitTest Vows
![Page 100: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/100.jpg)
Screw unit
JavaScript is madly in love with Testing.
Blue Ridge
JSpecQUnit JSUnit
JSTest
UnitestingYUI Test
jsUnitTest Vows
Jasmine
![Page 101: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/101.jpg)
JavaScript is madly in love with Testing.
http://pivotal.github.com/jasmine/
![Page 102: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/102.jpg)
describe('Array', function() { var array;
describe('#unique', function() { beforeEach(function() { array = [3,3,4]; });
it("returns only the unique values in the array", function() { expect(array.unique()).toEqual([3,4]); }); });});
Jasmine: BDD RSpec-like syntax
![Page 103: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/103.jpg)
Not mocks and Stubs
Spies
![Page 104: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/104.jpg)
function BetterArray(array){ this.array = array;}
BetterArray.prototype = { joinify: function(){ // Does some cool stuff this.array = this.array.join('.'); }}
Jasmine: Spies as Mocks
![Page 105: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/105.jpg)
describe('BetterArray', function() { var betterArray, anArray = [3,3,4];
beforeEach(function() { betterArray = new BetterArray(anArray); });
describe('#joinify', function() { it("calls the standard join method on Array", function() { var joinSpy = spyOn(betterArray.array, 'join'); betterArray.joinify(); expect(joinSpy).toHaveBeenCalled(); }); });});
Jasmine: Spies as Mocks
![Page 106: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/106.jpg)
describe('BetterArray', function() { var betterArray, anArray = [3,3,4];
beforeEach(function() { betterArray = new BetterArray(anArray); });
describe('#joinify', function() { it("calls the standard join method on Array", function() { var joinSpy = spyOn(betterArray.array, 'join'); betterArray.joinify(); expect(joinSpy).toHaveBeenCalled(); }); });});
Jasmine: Spies as Mocks
![Page 107: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/107.jpg)
describe('BetterArray', function() { var betterArray, anArray = [3,3,4];
beforeEach(function() { betterArray = new BetterArray(anArray); });
describe('#joinify', function() { it("calls the standard join method on Array", function() { var joinSpy = spyOn(betterArray.array, 'join'); betterArray.joinify(); expect(joinSpy).toHaveBeenCalled(); }); });});
Jasmine: Spies as Mocks
![Page 108: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/108.jpg)
describe('BetterArray', function() { var betterArray, anArray = [3,3,4];
beforeEach(function() { betterArray = new BetterArray(anArray); });
describe('#joinify', function() { it("calls the standard join method on Array", function() { var joinSpy = spyOn(betterArray.array, 'join'); betterArray.joinify(); expect(joinSpy).toHaveBeenCalled(); }); });});
Jasmine: Spies as Mocks
![Page 109: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/109.jpg)
AdvancedBrowser Patterns
![Page 110: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/110.jpg)
JavaScript still live into browser...
![Page 111: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/111.jpg)
for (var i = 0; i < 100; i += 1) { document.getElementById("result").innerHTML += i + ", ";}
Dom access: write
{antipattern}
![Page 112: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/112.jpg)
var i, content = "";for (i = 0; i < 100; i += 1) { content += i + ",";}document.getElementById("result").innerHTML += content;
Dom access: update local variable
{pattern}
![Page 113: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/113.jpg)
var padding = document.getElementById("result").style.padding, margin = document.getElementById("result").style.margin;
Dom access: read
{antipattern}
![Page 114: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/114.jpg)
var style = document.getElementById("result").style, padding = style.padding, margin = style.margin;
{pattern}
Dom access: read with local variable
![Page 115: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/115.jpg)
Dom manipulation
{antipattern}
// appending nodes as they are created var p, t;p = document.createElement('p'); t = document.createTextNode('first paragraph'); p.appendChild(t); document.body.appendChild(p);
p = document.createElement('p'); t = document.createTextNode('second paragraph'); p.appendChild(t); document.body.appendChild(p);
![Page 116: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/116.jpg)
Dom manipulation
{antipattern}
// appending nodes as they are created var p, t;p = document.createElement('p'); t = document.createTextNode('first paragraph'); p.appendChild(t); document.body.appendChild(p);
p = document.createElement('p'); t = document.createTextNode('second paragraph'); p.appendChild(t); document.body.appendChild(p);
![Page 117: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/117.jpg)
var p, t, frag;frag = document.createDocumentFragment();p = document.createElement('p'); t = document.createTextNode('first paragraph'); p.appendChild(t); frag.appendChild(p);
p = document.createElement('p'); t = document.createTextNode('second paragraph'); p.appendChild(t); frag.appendChild(p);document.body.appendChild(frag);
Dom manipulation
{pattern}
![Page 118: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/118.jpg)
Dom manipulation
{pattern}
var p, t, frag;frag = document.createDocumentFragment();p = document.createElement('p'); t = document.createTextNode('first paragraph'); p.appendChild(t); frag.appendChild(p);
p = document.createElement('p'); t = document.createTextNode('second paragraph'); p.appendChild(t); frag.appendChild(p);document.body.appendChild(frag);
![Page 119: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/119.jpg)
Dom manipulation
{pattern}
var p, t, frag;frag = document.createDocumentFragment();p = document.createElement('p'); t = document.createTextNode('first paragraph'); p.appendChild(t); frag.appendChild(p);
p = document.createElement('p'); t = document.createTextNode('second paragraph'); p.appendChild(t); frag.appendChild(p);document.body.appendChild(frag);
![Page 120: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/120.jpg)
Dom manipulation
{pattern}
var p, t, frag;frag = document.createDocumentFragment();p = document.createElement('p'); t = document.createTextNode('first paragraph'); p.appendChild(t); frag.appendChild(p);
p = document.createElement('p'); t = document.createTextNode('second paragraph'); p.appendChild(t); frag.appendChild(p);document.body.appendChild(frag);
![Page 121: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/121.jpg)
The place of <script> element<!doctype html> <html> <head> <title>My App</title> <script src="jquery.js"></script> <script src="jquery.quickselect.js"></script> <script src="jquery.lightbox.js"></script> <script src="myapp.js"></script> </head> <body> ... </body></html>
{antipattern}Worst
![Page 122: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/122.jpg)
<!doctype html> <html> <head> <title>My App</title> <script src="all_20110127.js"></script> </head> <body> ... </body></html>
The place of <script> element
{antipattern}
![Page 123: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/123.jpg)
The place of <script> element
{pattern}
<!doctype html> <html> <head> <title>My App</title> </head> <body> ... <script src="all_20110127.js"></script> </body></html>
![Page 124: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/124.jpg)
It’s just a beginning...
![Page 125: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/125.jpg)
Study
![Page 126: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/126.jpg)
![Page 127: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/127.jpg)
“Save it for a rainy day!”
![Page 128: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/128.jpg)
Check your code with jslint.com
![Page 129: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/129.jpg)
![Page 130: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/130.jpg)
![Page 131: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/131.jpg)
http://jashkenas.github.com/coffee-script/
![Page 132: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/132.jpg)
http://jashkenas.github.com/coffee-script/
It’s just JavaScript!
![Page 133: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/133.jpg)
# Assignment:number = 42opposite = true
# Conditions:number = -42 if opposite
# Functions:square = (x) -> x * x
# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x
# Splats:race = (winner, runners...) -> print winner, runners
var cubes, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};
math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};}
JavaScript
![Page 134: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/134.jpg)
# Assignment:number = 42opposite = true
# Conditions:number = -42 if opposite
# Functions:square = (x) -> x * x
# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x
# Splats:race = (winner, runners...) -> print winner, runners
var cubes, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};
math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};}
JavaScript
![Page 135: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/135.jpg)
# Assignment:number = 42opposite = true
# Conditions:number = -42 if opposite
# Functions:square = (x) -> x * x
# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x
# Splats:race = (winner, runners...) -> print winner, runners
var cubes, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};
math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};}
JavaScript
![Page 136: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/136.jpg)
# Assignment:number = 42opposite = true
# Conditions:number = -42 if opposite
# Functions:square = (x) -> x * x
# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x
# Splats:race = (winner, runners...) -> print winner, runners
var cubes, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};
math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};}
JavaScript
![Page 137: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/137.jpg)
# Assignment:number = 42opposite = true
# Conditions:number = -42 if opposite
# Functions:square = (x) -> x * x
# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x
# Splats:race = (winner, runners...) -> print winner, runners
var cubes, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};
math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};}
JavaScript
![Page 138: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/138.jpg)
# Assignment:number = 42opposite = true
# Conditions:number = -42 if opposite
# Functions:square = (x) -> x * x
# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x
# Splats:race = (winner, runners...) -> print winner, runners
var cubes, math, num, number, opposite, race, square;var __slice = Array.prototype.slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};
math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};}
JavaScript
![Page 139: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/139.jpg)
# Existence:alert "I knew it!" if elvis?
# Array comprehensions:cubes = (math.cube num for num in list)
if (typeof elvis != "undefined" && elvis !== null) { alert("I knew it!");}
cubes = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { num = list[_i]; _results.push(math.cube(num)); } return _results;})();
JavaScript
![Page 140: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/140.jpg)
# Existence:alert "I knew it!" if elvis?
# Array comprehensions:cubes = (math.cube num for num in list)
if (typeof elvis != "undefined" && elvis !== null) { alert("I knew it!");}
cubes = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { num = list[_i]; _results.push(math.cube(num)); } return _results;})();
JavaScript
![Page 141: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/141.jpg)
...but it’s just another story...
![Page 142: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/142.jpg)
![Page 143: JavaScript Survival Guide](https://reader034.vdocuments.mx/reader034/viewer/2022051322/54214ac27bef0a060c8b5ccc/html5/thumbnails/143.jpg)
@giordanoscalzo
www.slideshare.net/giordano
github.com/gscalzo
Roma,5 marzo 2011
http://creativecommons.org/licenses/by-nc-sa/3.0/