-
Notifications
You must be signed in to change notification settings - Fork 0
JavaScript the Good Parts
illyfrancis edited this page Nov 6, 2013
·
2 revisions
http://code.google.com/edu/languages/index.html#_js_goodparts
Douglas Crockford
The broadest range of programmer skills of any programming language
- from comp scientiests to cut-n-pasters and everyone in between
- JavaScript is not a language I know
- The browser programming experience is awful
- It's not fast enough
- DOM is slow, not JavaScript
- The language is just a pile of mistakes
- Self
- prototypal inheritance
- dynamic objects
- Scheme
- lambda
- loose typing
- Java
- syntax
- conventions
- Perl
- regular expressions
- global variables
-
- adds and concatenates
- semicolon insertion
- typeof
- object, null, array all evaluated as "object" which is wrong
- with and eval
- phony arrays
- in js, essentially implemented hash tables
- == and !=
- false, null, undefined, NaN
- '' == '0' // false
- 0 == '' // true
- 0 == '0' // true
- false == 'false' // false
- false == '0' // true
- false == undefined // false
- false == null // false
- null == undefined // true
- " \t\r\n " == 0 // true
-> caused by type coercion
value = myObject[name];
if (value == null) {
alert(name + ' not found.');
}
=> use ===, so change to
value = myObject[name];
if (value === undefined) {
alert(name + ' not found.');
}
- Objects can inherit from other objects
- Functions can be members of objects
- for..in statement mixes inherited functions with the desired data members (common source of errors)
- Design question: => issue is it shoudn't have been released
if (foo)
bar();
foo;
0.1 + 0.2 !== 0.3
- Lambda
- Dynamic Objects
- Loose Typing
- Object Literals
- Classical
- Prototypal
- Class free
- Example 0:25:50
var digit_name = function () {
var names = [...];
return function (n) {
return names[n];
}
}();
Which leads to module pattern
var singleton = function () {
var privateVariable;
function privateFunction(x) {
... privateVariable ...
}
return {
firstMethod: function (a, b) {
... privateVariable ...
},
secondMethod: function (c) {
... privateFunction() ...
}
};
}();
- Make an Object
- Object literal
- new
- Object.create
- call another power contructor
- Define some variables and functions
- these become private members
- Augment the object with privileged methods
- Return the object
function myPowerConstructor(x) {
var that = otherMaker(x);
var secret = f(x); // private
that.priviledFunc = function () {
... secrete x that ...
};
return that;
}
return
{
ok: false
};
vs
return {
ok: false
};
Bad style produces bad results
Warning! JSLint will hurt your feelings.
- ECMAscript Fourth Edition (ES3.1)
- ES4 - cancelled