Date post: | 29-Nov-2014 |
Category: |
Technology |
Upload: | jonathan-julian |
View: | 5,308 times |
Download: | 1 times |
JavaScript: The Good Parts@jonathanjulian
crockford.com
First, the Bad Parts
globals. optional ;truthy/falsy. scope.
Then, the Good Parts!
functions as objects. loose typing.dynamic objects. literals.
The Bad Parts
globals
• x = 1;
• var y = 1;
• x is in the global namespace
• YourAppName.x = 1;
line termination
• semi-colons are INSERTED by the parser
• optional semi-colons are not a “language feature”
• you are not warned
truthy / falsy
==
===
All of these are false
take advantage
• if (x)
• if (myobject.propery)
• if (myobject.property == null) // no, No, NO!
• test for exactly what you want to test for
• ...or not
• KNOW THE FALSY VALUESundefined, null, false, 0, ‘’, NaN
no block scope
• functions create scope
• blocks do not
The Good Parts!
functions are objects
• can be passed, returned
• have scope
• have closure
loose typing
• no compiler
• no type heirarchies
objects are dynamic
• members can be added to any object, any time
• helpful to attach attributes
object literals
• aka JSON
• { count: 2, prefix: ‘pre_’ }
• pass options to functions
• JSON as communication
• JSON as persistence
• etc
Know the bad parts are there...and avoid them
Learn and love the good parts!
tools
• FireBug
• JSLint
• spidermonkey / rhino / v8
takeaway
• think about vars and scope
• never use ==
• if (myobject) {
• jslint.com
JavaScript is a beautiful language
@jonathanjulianjonathanjulian.com