So, suppose I have the following constructor function, whose prototype I have modified like so:
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;
After I have made foo, I want to create a new constructor, bar(), that is like a sort super foo: it has all of the properties, prototpye info, and methods of foo, but it ALSO has some extra properties and methods sprinkled on top. Would the following code be the most elegant way to do this?
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 = foo(options);
this.propD_ = 'yet another thing';
this.propE_ = options.moreStuff;
}
bar.prototype.p3 = 3;
foo.prototype.testing = 'A test';
smallObj = foo()'
bigObj = bar();
After running that code, here is what I would expect to get
console.log(a.p3); //3
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_); //'something totally different'
Is this a correct way of "extending" the functionality of some existing constructor, to make a sort of "Foo Plus". Basically, I'd like for foo to continue to work exactly as it did before bar() came into existence, but for bar to be a set of properties and methods that are added on top of foo. Am I doing this right?