Javascript Enlightenment Review

This book is a good read for understanding javascript coding syntax, prototyping, scope, and closure.   Although brief and repetitive after Chapter 8, it clarifies several aspects of the language.  If you’re an intermediate to advance javascript developer, this book many not be useful to you.  The most important thing is javascript’s flexibility.  You only use primitives and objects; javascript uses wrappers automatically when needed.  The other parts are comparing, contrasting methods, instantiation, closure, scope, hoisting.  The author describes what matters is the way properties are accessed, mainly if using reserved keywords such as “class”.

The problem with NULLS (see example below):

[code]var myNull = null;
console.log(typeof myNull); // outputs ‘object’ NOT null[/code]

// Use === when comparing null or undefined, because == will not work.

[code]Foo.prototype.x = 1;
var FooInstance = new Foo();
console.log(FooInstance.x); // outputs 1
// now let’s replace/override the prototype object with a new Object() object
Foo.prototype = {x:2};
console.log(FooInstance.x) // outputs 1 not 2[/code]

Here are several specific concepts the book teaches:

Values are considered primitive when they are irreducible.

String, Number, and Booleans are considered primitives because Javascript creates a wrapper object associated with the method or property used, then discards the object.

arguments.length Properties

The arguments object has a unique length property.  It actually returns the number of parameters passed to the function during invocation, not what is defined (myFunction.length).

[code]myFunction(); //logs 0    myFunction(a, b, c); //logs 3[/code]

arguments.callee Property

A reference to the function being called.

[code]function foo() {  alert(arguments.callee) };  // logs foo()[/code]

Anonymous Functions

Not given an identifier.  Mostly used for passing functions as a parameter to another function.

Self-Invoking Function Expression

[code]var myFunc = function() { alert(‘abc’) } ( );[/code]

Functions can be invoked after definition using the parantheses ( ).

According to ECMA, parentheses around the function are required if to be invoked immediately.

The Head/Global Object

All implementations of Javascript require the use of a single head object.  This is called “window” in browser environments.

The head object is implied and not referenced explicitly.

Using implied properties/methods runs code faster “alert( )” rather than being explicit “window.alert( )”

The THIS keyword refers to the head object in nested functions.

-Nested functions after the first function log as “window’ objects.

-THIS value will always be a reference to the head object when encapsulated inside another function.

call( ) and appy () methods

[code]myFunction.call( myObject, ‘foo’, ‘bar’ );  // set values to object[/code]

Using call( ) the parameters are separated by commas.

[code]muFunction.apply( myObject, [‘foo’, ‘bar’] ); // set values to object[/code]

Using appy( ) the parameters are passed inside an array.

The scope chain is the same as the prototype chain. A set of systematic and hierarchical set of locations.

An object’s custom prototype should not be replaced or updated with new properties once you start creating its instances.

If that doesn’t explain it for you then you should read this book for yourself.