javascript - Do we really have to change to Object.create? -
i have question you
as know, new
operator works way
function fakenew(ctor) { var instance = object.create(ctor.prototype); instance.constructor(); // skip conditional simplicity return instance; }
it begin when did object.create
polifill create objects without using new
.
object.create = function(proto) { function f() { } f.prototype = proto; return new f(); }
i started creating lot of objcts , prototiping them, many times need initializa properties did .init()
method them
var base = { init: function() { emittermixin.call(this); return this; } }; var obj = object.create(base).init();
but of course, had rembember return this
.init()
, many times forget or worst, forgot call .init()
. trying simplify object initialization wanted create global helper it: invoke object.create
, invoke initializer function , return this.
function create(proto) { var child = object.create(proto); child.init(); return child; }
then when wanted hit head against wall when realized doing virtually what new
operator does lot slower. , in case polifill applies use new
under hood.
so ask myself, benefits throwing new
? it's notably quicker on main browsers , because of javascript nature need call initualizer function, new
it's begining.
and worst, i've noticed i used 2 different types of objects, "abstract" , "instances", bigger difference instances had invoke .init()
while abstracts not necesasary because used prototypes of other objects. , i've seen pattern while using new
:
function foo() { } foo.prototype.method = function() { ... }; function bar() { } // "abstract" doesnt invoke initializer bar.prototype = object.create(foo.prototype); bar.prototype.other = function() { ... }; // "instance", constructor called initializer var obj = new bar();
is there benefit if stop seeing new
? sure it's not higher-level tool simplifies we'll have anyway? pros/cons see new
, object.create
?
i have tried both approaches , don't see problem traditional approach using new
. main argument object.create
makes prototypal inheritance more clear - don't have understand intricacy of prototype
property on function constructors , how relates actual prototype of object. require initialization separate step.
my preferred approach combine 2 approaches, e.g.:
function animal(name) { this.name = name || null; } animal.prototype.eat = function() { console.log('yum'); } function cat(name) { animal.call(this, name); } cat.prototype = object.create(animal.prototype); cat.prototype.constructor = cat; cat.prototype.meow = function() { console.log('meow'); } var garfield = new cat('garfield'); garfield.eat(); garfield.meow();
object.create
better inheritance here cat.prototype = new animal()
2 reasons:
- if wanted require
name
parameter (throwing exception if wasn't passed), , inheritance (cat.prototype = object.create(animal.prototype)
) still work. - if forgot call
cat
constructoranimal
constructor, of properties have been createdanimal
constructor undefined instead, allowing realize mistake.
although approach (using constructor functions , new
) less "purely" prototypal, it's still prototypal inheritance , long understand how use correctly works fine, , requires little less typing object.create
approach.
i wrote other notes in documentation javascript oo library, simpleoo. (note: may changing api library; i'm linking because documentation discusses of these concepts in more detail.)
Comments
Post a Comment