I would like to include the properties and initialization logic from a "base" type into a "sub" type in JavaScript. Is the following idiomatic?
function Base(arg1) {
this.foo = arg1.foo;
}
function Sub(arg1) {
//Initialize using the base ctor...
Base.call(this, arg1);
}
Yes (but just
this.foo = arg1
in theBase
, notarg1.foo
), that's the typical way you set up a base/derived relationship with JavaScript's prototypical inheritance and constructor functions. Here's the complete pattern:I have a script,
Lineage
, that makes it more declarative, encourages private resources, and makes calls to parent "class" methods easier. But that script will be obsoleted by ES6's classes, which despite being called classes are still prototypical in nature. (And there are transpilers for ES6's class syntax that generate ES5 code, so you can use the new syntax now, it just means you need a build step before using the result.)If you need to support really old browsers (like IE8) that don't have
Object.create
, you can shim the one-argument version of it used above:Note that constructor functions are just one way you can use prototypical inheritance in JavaScript, not the only way. But if you're using constructor functions, that's the pattern.