Best Way To Create A New Constructor That Relies On An Old One Using Prototypes
So, suppose I have the following constructor function, whose prototype I have modified like so: function foo(options) { this.propA_ = 'whatever'; this.propB_ = 'something'; t
Solution 1:
Ok, I finally worked through the discussion I have in the comment thread in response to the quesion, and this is the answer I came up with. I'll repost the code here - thank you to everyone who helped me work through this!
functionfoo(options) {
this.propA_ = 'whatever';
this.propB_ = 'something';
this.propC_ = options.stuff;
this.randomMethod = functionomg() {
/*code etc etc etc*/
};
}
foo.prototype.p1 = 1;
foo.prototype.p2 = 2;
functionbar(options) {
//this = new foo(options);var parent = newfoo(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 = newfoo(myOpts);
var bigObj = newbar(myOpts);
console.log(smallObj.p2); //2console.log(bigObj.p2); //2
bigObj.p2 = 100;
console.log(bigObj.p2); //100console.log(foo.prototype.p2); //2//console.log(bigObj.randomMethod()); //Will workconsole.log(smallObj.p3); //undefinedconsole.log(smallObj.propA_); //'whatever'console.log(bigObj.propA_); //'whatever'
foo.prototype.propA_ = 'something totally different';
console.log(bigObj.propA_); //'whatever'
Solution 2:
Not sure why you have so much code to have prototype inheritance. You can use something like goog.inherit (and goog.base if you plan to use closure compiler to compile) from the closure library.
Here is some sample code that uses goog.inherit:
var goog = {};
/**
* Inherit the prototype methods from one constructor into another.
* @param {Function} childCtor Child class.
* @param {Function} parentCtor Parent class.
*/
goog.inherits = function (childCtor, parentCtor) {
/** @constructor */functiontempCtor() { };
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = newtempCtor();
childCtor.prototype.constructor = childCtor;
};
/** @constructor */varGrandParent = function (arg1) {
window['console'].log("grandparent constructor called with arg1:", arg1);
}
GrandParent.prototype.doSomething = function () {
return"From GrandParent";
}
/** @constructor */varParent = function (arg1, arg2) {
GrandParent.call(this, arg1);
window['console'].log("parent constructor called with arg1:", arg1);
window['console'].log("parent constructor called with arg2:", arg2);
}
goog.inherits(Parent, GrandParent);
/** @override */Parent.prototype.doSomething = function () {
returnParent.superClass_.doSomething() + " From Parent";
}
/** @constructor
* @extends Parent */varChild = function (arg1, arg2, arg3) {
Parent.call(this, arg1, arg2);
window['console'].log("child constructor called with arg1:", arg1);
window['console'].log("child constructor called with arg2:", arg2);
window['console'].log("child constructor called with arg3:", arg3);
}
goog.inherits(Child, Parent);
/** @override */Child.prototype.doSomething = function () {
returnChild.superClass_.doSomething() + " From Child";
}
var c = newChild("arg1", "arg2", "arg3");
console.log(c.doSomething());
Post a Comment for "Best Way To Create A New Constructor That Relies On An Old One Using Prototypes"