JSFiddle - React, Tailwind, and code Playground
JavaScript
function foo(options) {
this.propA_ = 'whatever';
this.propB_ = 'something';
this.propC_ = options.stuff;
this.randomMethod = function omg() {
/*code etc etc etc*/
};
}
foo.prototype.p1 = 1;
foo.prototype.p2 = 2;
function bar(options) {
//this = new foo(options);
var parent = new foo(options);
this.prototype = parent.prototype;
for (var x in parent) {
if (parent.hasOwnProperty(x)) {
console.log('iterating past'+x);
this[x] = parent[x];
}
}
this.propD_ = 'yet another thing';
this.propE_ = options.moreStuff;
}
// Make `bar` inherit from an instance of `foo`
bar.prototype = Object.create(foo.prototype);
// Add properties to the bar prototype
bar.prototype.p3 = 3;
// Not sure what you were doing here
//foo.prototype.testing = 'A test';
var myOpts = {
stuff: 'a cat',
moreStuff: 'many dogs'
};
var smallObj = new foo(myOpts);
var bigObj = new bar(myOpts);
console.log(smallObj.p2); //2
console.log(bigObj.p2); //2
bigObj.p2 = 100;
console.log(bigObj.p2); //100
console.log(foo.prototype.p2); //2
//console.log(bigObj.randomMethod()); //Will work
console.log(smallObj.p3); //undefined
console.log(smallObj.propA_); //'whatever'
console.log(bigObj.propA_); //'whatever'
foo.prototype.propA_ = 'something totally different';
console.log(bigObj.propA_); //'whatever'