I am reading “Javascript : The good Parts” to improve my Javascript bases and knowledge. I was reading stuff about prototype
and I encountered in this function :
var stooge = { ... }
if(typeof Object.create !== 'function'){
Object.create = function(o) {
var F = function () {};
F.prototype = o;
return new F();
};
}
var another_stooge = Object.create(stooge);
I didn’t really get the meaning and the benefit of creating this function.
It’s only a shim for the standard Object.create function which isn’t available in some browsers (yes, you guessed which one).
Note that it’s not complete : it doesn’t do everything that is done by Object.create
but it’s probably enough for the author.
As for why the author wanted to use Object.create
instead of a explicit prototype based OOP, it’s probably because it’s a little simpler.