jslent: give it up for javascript

16
JSLent give it up for JavaScript Tuesday, March 22, 2011

Upload: bigbluehat

Post on 22-Apr-2015

1.215 views

Category:

Self Improvement


2 download

DESCRIPTION

Simple ways to improve your JavaScript code.

TRANSCRIPT

Page 1: JSLent: give it up for JavaScript

JSLentgive it up for JavaScript

Tuesday, March 22, 2011

Page 2: JSLent: give it up for JavaScript

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

eschew eval

Tuesday, March 22, 2011

Page 4: JSLent: give it up for JavaScript

how prototypical

Tuesday, March 22, 2011

Page 5: JSLent: give it up for JavaScript

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

getting it rightbuy the book

Tuesday, March 22, 2011

Page 7: JSLent: give it up for JavaScript

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

He begat JSLint

• cleans up your JS

• will hurt your feelings

Tuesday, March 22, 2011

Page 9: JSLent: give it up for JavaScript

(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

(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

see?

Tuesday, March 22, 2011

Page 12: JSLent: give it up for JavaScript

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

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

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

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

Resources

• jslint.com

• jshint.com

• ejohn.org/apps/learn/

Tuesday, March 22, 2011