Correct javascript inheritance -


i've been reading lot of articles "inheritance" in javascript. of them uses new while others recommends object.create. more read, more confused since seems exist endless amount of variants solve inheritance.

can kind show me accepted way (or defacto standard if there one)?

(i want have base object model can extend restmodel or localstoragemodel.)

simple: object.create not supported in environments, can shimmed new. apart that, 2 have different aims: object.create creates object inheriting other, while new also invokes constructor function. use appropriate.

in case seem want restmodel.prototype inherits model.prototype. object.create (or shim) correct way then, because not want a) create new instance (instantiate new model) , b) don't want call model constructor:

restmodel.prototype = object.create(model.prototype); 

if want call model constructor on restmodels, has nothing prototypes. use call() or apply() that:

function restmodel() {     model.call(this); // apply model's constructor on new object     ... } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -