jslent: give it up for javascript
DESCRIPTION
Simple ways to improve your JavaScript code.TRANSCRIPT
JSLentgive it up for JavaScript
Tuesday, March 22, 2011
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
eschew eval
Tuesday, March 22, 2011
how prototypical
Tuesday, March 22, 2011
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
getting it rightbuy the book
Tuesday, March 22, 2011
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
He begat JSLint
• cleans up your JS
• will hurt your feelings
Tuesday, March 22, 2011
(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
(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
see?
Tuesday, March 22, 2011
JSLint: finds...stuff
• Undefined Vars & Functions
• Globals/Members lists
• Semicolon checking
• clean blocks
• checks for break in switch
Tuesday, March 22, 2011
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
Awful Parts
• Global Variables
• Scope (confusing/misleading variable scope)
• Semicolon Insertion
• Reserved Words
Tuesday, March 22, 2011
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
Resources
• jslint.com
• jshint.com
• ejohn.org/apps/learn/
Tuesday, March 22, 2011