I just watched an introductory JavaScript lecture from Douglas Crockford, in which he mentions a function called object that should be used to create a new object linked to an object as its parameter. I would guess what he means by that is that if I say
var objB = object(objA);
The objB‘s internal [[prototype]] reference is set to objA, although he didn’t explicitly formulate it like that.
On the other hand, I have read his book, in which he doesn’t mention such a function at all and instead presents his own way of creating an object from a prototype, defining following function :
Object.create = function(o) {
var F = function() {};
F.prototype = o;
return new F();
}
Essentially taking advantage of the behavior of the new operator that sets the internal [[prototype]] link of the newly created object to whatever the constructor function’s prototype property points to.
My question is why would he omit a built-in function and invent his own way to do the same thing. Is the former call to object function really equivalent to
var objB = Object.create(objA);
Or is there some slight difference ?
The two functions are one and the same, and neither are built in to JavaScript. See Crockford’s article describing why he switched between the different naming conventions.
Edit from the future: I saw this old answer and wanted to point out that Object.create() is indeed a native (and very important) ES5 method.