Notes from Douglas Crockford's "The JavaScript Programming Language" Video
Crockford is a JavaScript wizard from Yahoo! who has a number of excellent videos about JavaScript. I found the series "The JavaScript Programming Language" linked with the text "You think you know JavaScript?" That title was accurate. It was humbling, but very informative. If you do any serious JavaScript work, you need to watch these.
- The JavaScript Programming Language - 1 of 4
- The JavaScript Programming Language - 2 of 4
- The JavaScript Programming Language - 3 of 4
- The JavaScript Programming Language - 4 of 4
If you'd rather not spend 2 hours watching, here is a summary of what I felt were the most important points.
Floating-Point Math
There is no integer type in JS, only "number". Watch for floating-point weirdness. Example:
Comparisons and Conditionals
"==" ignores type in comparisions while "===" will compare type as well as value.
Conditionals will return true or false based on the "truthiness" of the value. "!!" will return the true boolean. "Falsey" values are:
Everything else returns true, including:
Scope
There are two scopes in JavaScript: global and function. If you don't declare a variable, it assumes global.
function testFunc1() {
ii = 42;
}
If a variable is declared anywhere in the function, it is of function scope.
var jj = 0;
function testFunc2() {
jj = 42;
//Don't declare your variables like this.
//It is only an example to illustrate that variables declared anywhere in the function have function scope
var jj;
}
Objects
Q: Is there a dictionary object (associative array, map, hash, dictionary, lookup table) in JavaScript?
A: All objects are dictionary objects.
Names need not be quoted. Use dot or subscript notation to reference.
var dictObj = {
a: 1,
"b": 2
}
He liked to throw around using the "object(o)" function a lot when talking about inheriting and extending objects. He didn't point out until the third video that it won't work. (It is a custom function he wrote.) I am pretty sure that "new Object(o)" works the same way natively.
var testObj = new Object(dictObj);
testObj['c']='x';
Inner Functions
You can declare functions inside a function. He believes that this is one of the most powerful aspects of JavaScript and one of the main reaasons to take it seriously as a language. Variables they use will be statically (as opposed to dynamically) scoped. I am not 100% certain what that means, but here is an example:
function countDown() {
var oCntr = document.getElementById("cntr");
var cStart = 9;
//Once this function is fired off, it will remember the cStart value.
function decrement() {
oCntr.innerHTML = cStart;
if (cStart > 0) {
cStart--;
setTimeout(decrement, 200);
}
}
setTimeout(decrement, 200);
}
Function Arguments
Any arguments that are not included in calling the function will be undefined. You can use the || (default operator) to assign a value to undefined variables.
function testDef(a, b) {
b = b || 3;
return b;
}
All extra arguments are ignored.
Or if you want, pass as many arguments as you wish. You can access them in the arguments array.
function addStuff() {
var total = 0;
for (var ii = 0; ii < arguments.length; ii++) {
//Not only does this iterate through arguments, it uses the + to force addition instead of concatenation
total += +arguments[ii];
}
return total;
}
