jslent: give it up for javascript
DESCRIPTION
Simple ways to improve your JavaScript code.TRANSCRIPT
![Page 1: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/1.jpg)
JSLentgive it up for JavaScript
Tuesday, March 22, 2011
![Page 2: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/2.jpg)
Whither JS?
• In the browsers (obviously)
• On the server
• node.js (V8)
• narwhal.js (Rhino & friends)
• In your database
• if it’s CouchDB
Tuesday, March 22, 2011
![Page 3: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/3.jpg)
eschew eval
Tuesday, March 22, 2011
![Page 4: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/4.jpg)
how prototypical
Tuesday, March 22, 2011
![Page 5: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/5.jpg)
function Person(){}
Person.prototype.getName = function(){
return this.name;
};
function Me(){
this.name = "John Resig";
}
Me.prototype = new Person();
var me = new Me();
me.getName(); // "John Resig"
http://ejohn.org/apps/learn/#78
Tuesday, March 22, 2011
![Page 6: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/6.jpg)
getting it rightbuy the book
Tuesday, March 22, 2011
![Page 7: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/7.jpg)
Meet Douglas Crockford
• author of JavaScript: the Good Parts
• minified JS education
• works at Yahoo!
• checkout YUI Theatre
• also javascript.crockford.com
Tuesday, March 22, 2011
![Page 8: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/8.jpg)
He begat JSLint
• cleans up your JS
• will hurt your feelings
Tuesday, March 22, 2011
![Page 9: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/9.jpg)
(function() {
four = eval('2+2');
var eight = multiply(four, 2);
function multiply(n,y) {
return n*y
}
if (typeof eight == 'string') {
console.log('fale');
}
});
Tuesday, March 22, 2011
![Page 10: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/10.jpg)
(function() {
four = eval('2+2');
var eight = multiply(four, 2);
function multiply(n,y) {
return n*y
}
if (typeof eight == 'string') {
console.log('fale');
}
});
Problem at line 2 character 8: eval is evil.
'multiply' was used before it was defined.
Expected ';' and instead saw '}'.
Expected '===' and instead saw '=='.
Do not wrap function literals in parens unless they are to be immediately invoked.
Expected an assignment or function ca# and instead saw an expression.
Tuesday, March 22, 2011
![Page 11: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/11.jpg)
see?
Tuesday, March 22, 2011
![Page 12: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/12.jpg)
JSLint: finds...stuff
• Undefined Vars & Functions
• Globals/Members lists
• Semicolon checking
• clean blocks
• checks for break in switch
Tuesday, March 22, 2011
![Page 13: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/13.jpg)
more stuff
• singe var at the top
• no more with
• better comparisons
• == vs ===
• eval is Evil (but you knew that already)
Tuesday, March 22, 2011
![Page 14: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/14.jpg)
Awful Parts
• Global Variables
• Scope (confusing/misleading variable scope)
• Semicolon Insertion
• Reserved Words
Tuesday, March 22, 2011
![Page 15: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/15.jpg)
Reserved Words• boolean
• break
• byte
• case
• catch
• char
• continue
• default
• delete
• do
• double
• else
• false
• final
• finally
• float
• for
• function
• if
• in
• instanceof
• int
• long
• new
• null
• return
• short
• switch
• this
• throw
• true
• try
• typeof
• var
• void
• while
• with
Tuesday, March 22, 2011
![Page 16: JSLent: give it up for JavaScript](https://reader037.vdocuments.us/reader037/viewer/2022102923/5538cc3f550346bb318b488a/html5/thumbnails/16.jpg)
Resources
• jslint.com
• jshint.com
• ejohn.org/apps/learn/
Tuesday, March 22, 2011