Javascript is Weird
- users have the widest range of skill levels of any programming language, computer scientists to cut-and-paste
- javascript has good parts in with the mistakes, proof: is succeeding where java failed
- has many language influences: Self (prototypal inheritance, dynamic objects), Scheme (lambda), Java (syntax), Perl (regex)
The Bad Parts
- global variables (especially default global variables, if you leave out “var”)
- overloading of + (used for addition and string concatenation)
- semicolon insertion (on error, goes back and inserts a semicolon on a previous linefeed)
- typeof works funky sometimes, like with arrays
- with and eval are dangerous
- phony arrays that are really objects
- == and != do type coercion (=== and !== don’t)
- false, null, undefined, NaN all exist
- for..in includes inherited members along with direct object members… and they didn’t tell anybody
Bad Heritage
- this is stuff that C got wrong and everyone else did too
- blockless one-line statements
- expression statements without assignment or function calls (useless)
- floating point arithmetic is imprecise (should scale to integer vals and then do ops)
- ++ and –, danger of buffer overflows, etc.; dc doesn’t use them *(Bri sez: Not sure I understand why, or agree.)
- switch does automatic fall-through
Good Parts
- lambda: concept that functions are first-class values (an idea from Scheme)
- js is the first mainstream lambda language
- dynamic objects (no classes as templates, prototype can be changed later)
- loose typing
Inheritance
- classical (basically everything else) vs. prototypal (javascript)
- class-free, objects inherit from other objects by linking to them (known as delegation / differential inheritance)
- DC shows his object copying function, like from the YUI Theater videos
- new required with constructor (see notes from yesterday)
- if you don’t include it, no error; and the global object is overwritten by the constructor (!)
- obviously this is Bad
A Module Pattern
- “closure” means every function has a reference to the environment in which it was created
- shows the closure example w/private variables, way to avoid global variables
- function can return a function that returns the desired values
- or function can return an object that contains “public” methods/properties
- private stuff goes above the return, nothing outside of that outer function has access to it
A Power Constructor Pattern
- function is not capitalized, not called with ‘new’; not directly itself a constructor
- make an object, “that” (because can’t use “this”)
- define some variables and functions in the function; these are private
- augment the “that” object with privileged methods that access the private stuff
- return “that”
Misc.
- talks about his project, JSLint; says it “defines a professional subset of javascript”
- “you’re not expressing yourself by writing crap[py code]”; creativity has to involve clean, robust code
- “perfectly fine == faulty” *(Bri sez: but only with type coercion, apparently)
- “Javascript: no new design errors since 1999!”
(Note: Ultimately, I like Javascript because you can do cool stuff with it, and because it’s open and standards-based. But in some ways I think the obscure features and weirdnesses make me like it too.
Javascript is like your weird little cousin that most people don’t really get along with, but if you know how to deal with him he’s actually really cool, and then you get to feel special.)