Upload
tranmien
View
241
Download
2
Embed Size (px)
Citation preview
Copyright © 2012-2013 by Object Computing, Inc. (OCI). All rights reserved.
JavaScript Grievancescommon complaints
about JavaScript
Seinfeld Festivus Airing of Grievanceshttp://www.youtube.com/watch?v=xoirV6BbjOg
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
Assessment Key
E - Addressed by EcmaScript 6 (ES6)
D - Doesn’t really happen in practice
L - Caught by Linting tools (JSLint/JSHint) and/or ES5 strict mode
P - Personal preference
R - Real problem
Y - You can easily learn how to deal with this
2
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
Crockford’s Bad Parts ...
Implied globals (E, L)assign to a variable without a var and it becomes global
can’t do in strict mode
in ES6, use let instead of var
Scope (E, Y)variables are scoped to a function body or they are global
no block scope in things like if statements and loops (see let in ES6)
CoffeeScript and Node.js provide file/module-scoped variables
Semicolon insertion (D, L)semicolons are optional
when omitted, they are automatically inserted, sometimes not where intended
hard to get wrong though
3
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
... Crockford’s Bad Parts ...
Reserved words (L)many aren’t currently used, but reserved for possible future use
don’t have to memorize them; let a lint tool warn you
Unicode (E, D)uses UTF-16 characters, not UTF-8, and string methods expect only 2-byte characters, not 4
characters in all modern languages only require 2 bytes
typeof (Y)
typeof null === 'object' and typeof [1, 2, 3] === 'object'
Numbers (E, D)all are represented by a double; no integer type,but can accurately represent up to 53 bitswhich is large enough for almost all uses
4
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
... Crockford’s Bad Parts
Arrays (E, D)represents arrays as objects where keys are string versions of indexes
a potential performance issue; but not typically
ES6 adds typed arrays of numbers
Falsy values (Y)false, undefined, null, 0, NaN and ""are all treated as false in boolean contexts
everything else is truthy
== vs. === and != vs. !== (L)short forms perform type coercion, long forms do not
eval(code) (D, L)need to be careful about source of code
5
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
More Complaints ...
Implicit type coercions (D, L)Google “Gary Bernhardt WAT”
Verbose function keyword (E, R)
can use arrow functions in ES6
Object keys must be strings (E, R) can use Map and Set collections in ES6
No fancy collections (D, E, R)like sets and maps, only Array and Object
can use Map and Set collections in ES6
6
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
... More Complaints ...
Prototypal inheritance (E, R, Y)many steps to get right
call superclass ctor in subclass ctor
set subclass .prototype to an instance of superclass
set subclass .prototype.constructor to subclass ctor
can use class and extends keywords in ES6
Ability to add/override functions and methods (D)far away from other related definitions
a.k.a. monkey patching
Dynamically typed (P)some prefer static typing
can use TypeScript
7
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
... More Complaints
No require/import/include capability (E, R)each client-side JavaScript file must be referenced from a script tag in HTMLor use something like RequireJS
Node provides CommonJS-style require
ES6 provides modules
Need to learn async programming style (Y)callback functions and avoiding deeply nested calls
Supports mutable things (P)can use ClojureScript
Don’t like braces, parens and semicolons (P)can use CoffeeScript
8
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
Good Parts ...
First-class functionsstore in variables, pass to other functions, return from other functions
a necessary feature for functional programming
Closuresfunctions capture data in the scope where they are defined
Anonymous functions
Functions are objectscan attach properties with any kind of value, even other functions
9
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
... Good Parts ...
Objects are like mapscan add arbitrary properties whose values are data and functions
JSON serializationJSON.stringify(value) and JSON.parse(string)
JavaScript experience carries over toserver-side programming using Node.js
Can use CoffeeScript, ClojureScript and other languagesthat compile to JavaScript instead
10
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
... Good Parts
JavaScript IS THE ONLY PROGRAMMING LANGUAGE SUPPORTED BY ALL POPULAR WEB BROWSERS!
11
JS GrievancesCopyright © 2012-2014 by Object Computing, Inc. (OCI). All rights reserved. 10 -
Stop the Hate!
Typically Twitter commentsOH: JavaScript sucks ... ha, ha, ha!
TIL: JavaScript is lame.
TruthYou can implement nearly any application successfully in nearly any programming language.
Everyone has personal preferences.
There will never be a programming language that is everyone’s favorite.
Hate fragments the community and reduces opportunities for learning from each other.
12