Abstract

Notes on Douglas Crockford's Yahoo talk on Javascript

Table of Contents

Javascript

Strings

Booleans

boolean(value) similar to !! prefix.

Falsy Values

All other values (including objects are truthy):

Values

Undefined

Objects

Types

It is not untyped. It is loosely typed but it is very robust.

Functions can be values

Identifiers

Operators

+ used to concatenate as well as add. Also as a unary operator to convert string to number +"42" = 42. Or Number('42') = 42 Or parseInt("42", 10) = 42 or `+“3” + (+“4”) = 7

Equal == and not equal != operators. Unfortunately these can do type coercion. Better to use === and !==.

&& “logical and operator” also known as the “gaurd operator” (28:25).

|| “logical OR” or “default operator”

! & !! Prefix logical not operator

Bitwise operators

If you don’t intend for a bitwise operation, don’t use them.

Statements

Break

Labeled Break:

loop: for (;;) {
    if (…) {
        break loop;
    }
}

for loop

for arrays

for in

For objects

Will also go through all the keys that the object inherits from (unfortunately).

To mitigate this you need to:

for (var name in object) {
    if (object.hasOwnProperty(name)) { // this checks to see if the object actually has the property name.

    }
}

Switch

Advantages

Throw, try, catch

//constructor
throw new NameOfException(reason);

// or an object literal
throw {
    name: exceptionName,
    message: reason
}

try {
    // code
} catch (e) {
    switch (e.name)
}

Exceptions built into JS

with

It uses constructs in which you can’t predict what the program will do

var

Scope

return

return expression;
return; //returns undefined except for constructors then the default value is this

Objects

Collections

Object Literals

Maker function

function maker(name, where, grade, level) {
    var it = {};  // make a new empty object
    it.name = name;
    it['goto']  = where;
    it.grade = grade;
    it.level = level; 
    return it;
}

myObject = maker('Jack', 'jail', 'a', 3);

Object Augmentation

Linkage

Prototypal inheritance

Object Methods

Making a New Empty Object

All do the same thing:

new Object()

{} // prefered form

object(Object.prototype)

Reference

Delete

Use to delete members from an object:

delete myObject[name]; // actually changes it to undefined

Arrays

Literals

Methods

Deleting Elements

Removes the element, but leaves a hole in the numbering.

delete array[number]; // sets undefined for that index

Removes the element and renumbers all the following elements

array.splice(number, 1);

When to use

Distinguishing Arrays

* Neither of these work when the value comes from a different frame.

value.constructor === Array

value instanceof Array

Arrays and Inheritance

Functions

Function Operator

function name(params) { //statements }

Scope

Closure

If you find yourself using more than a couple of parameters. Use an object or an object literal instead since they are much more forgiving, substitutions can be made dynamically, etc.

MISC

Garbage Collection