I’ve been reading a lot of articles about “inheritance” in javascript. Some of them uses new while others recommends Object.Create. The more I read, the more confused I get since it seems to exist an endless amount of variants to solve inheritance.
Can someone be kind to show me the most accepted way (or defacto standard if there is one)?
(I want to have an base object Model which I can extend RestModel or LocalStorageModel.)
Simple:
Object.createis not supported in all environments, but can be shimmed withnew. Apart from that, the two have different aims:Object.createjust creates a Object inheriting from some other, whilenewalso invokes a constructor function. Use what is appropriate.In your case you seem to want that
RestModel.prototypeinherits fromModel.prototype.Object.create(or its shim) is the correct way then, because you do not want to a) create a new instance (instantiate anew Model) and b) don’t want to call the Model constructor:If you want to call the Model constructor on RestModels, that has nothing to do with prototypes. Use
call()orapply()for that: