Douglas Crockford's title and opening remarks were complimentary to JS, emphasising that there are very good parts to the language, as evidenced by the range of users, from computer science graduates to cut'n'paste coders. JavaScript is succeeding in an environment where Java failed.
Good things include prototype inheritance, 1st class functions (lambda) and loose typing. Closures work because of lambdas and garbage collection and is the best thing in programming languages
.
Bad things are:
- globals (particularly user-defined and overwritable)
- semicolon insertion
typeof object==typeof array==typeof null
!- with/eval
use of eval is a cry for help
phony arrays
(performance)- type coercion (==)
- too many negatives (
null, undefined, false, NaN
) why? E.g.object[name]==null
is commonly used but gives false +ve with0, null, ''
property values – must useobject[name]===undefined
. - C heritage, optional braces + permitted empty statements.
- Use of IEEE floats whereby 0.1+0.2 !== 0.3
- Dense constructions like ++ and –-
- Switch/case fall-through
new
– forget it and assignments overwrite the constructor
These are mainly things that can have disastrous consequences from minor typos but oftentimes seem useful.
Advocated JSLint.com and the professional subset
of JS that it permits but warned JSLint will hurt your feelings
and unlearning is hard
.
Style isn’t subjective
, e.g.return {
ok: true
}
vsreturn
{
ok :true
}
;-insertion means the second returns null
, ok
is a label and produces no syntax error and the braces wrap an empty statement that does nothing but is permitted…
Douglas’ perspective is to make JS more like JS should be
. Deprecate the worst features, don't break the syntax, keep it simple, keep it safe
(a subtle LOTR reference ;-). Add JSON.stringify, JSON.parse, safe/restricted eval
, add a "don’t enumerate" flag to objects, no experimental features
(hard to remove), no radical changes & no new syntax. More languages: two small languages are better then one large language
.
More security but that would break current JS syntax. JSLint "ad safe" mode c.f. Google Caja (Caja allows this
; JSLint does not.)
(This is a long post as lots of it was using notation I'm familiar with, hence my notes were legible and concise, but personally I'm with the JS2 pack: I teach students who learn Java first so if I can use Java-like syntax, so much the better, but DC's arguments are worth paying attention to.)
No comments:
Post a Comment