Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Sunday, November 15, 2009

Object.create

In order to create objects nicely in JavaScript, Crockford suggests:

if (typeof Object.create !== 'function') {
Object.create = function () { // create a method of the universal object
function F() {} //create an empty function
F.prototype = o; //load the function's prototype with the old object to inherit from
return new F(); //use that function to create a new object and return it.
};
}

This implements inheritance from an existing object when creating a new object.
Crockford says changing o later will change new object.

I also need to create a little javascript lab, a notebook to record code, and to look back on.