Reputation: 131
Say I have a class Car.
/**
*
* @constructor
* @extends {Component}
*/
obj.Car = function(prop1, prop2) {
goog.base(this, prop1);
this.prop2 = prop2;
};
goog.inherits(obj.Car, Component);
/**
*
* @return {obj.Car}
*/
obj.Car.create = function(a,b,c) {
var prop1 = obj.Car.createProp1(a,b,c);
var prop2 = obj.Car.createProp2(a,b,c);
return new obj.Car(prop1, prop2);
}
Cool. Now I want to sublcass car. Call it SuperCar.
obj.SuperCar = function(prop1,prop2) {
goog.base(this, prop1, prop2);
};
goog.inherits(obj.SuperCar, obj.Car);
How do I write the factory create method for SuperCar so that another class can also extend SuperCar?
So, ideally:
/**
* @return {obj.SuperCar}
*/
obj.SuperCar.create() = function() {
return /** type {obj.SuperCar} */ Obj.Car.create.apply(this, arguments);
};
However, this means I have to change obj.Car to:
/**
* @this {*) ????????? only this works
* @return {obj.Car}
*/
obj.Car.create = function(a,b,c) {
var prop1 = obj.Car.createProp1(a,b,c);
var prop2 = obj.Car.createProp2(a,b,c);
return new this(prop1, prop2);
};
This Javascript actually runs uncompiled and compiles. But I get a TypeError when running it compiled. Does anyone know the best solution for this? I've tried a few thing but can't seem to get it right.
Upvotes: 2
Views: 511
Reputation: 85
I will assume that this is the base class.
SuperCar.js
/**
* @constructor
*/
obj.SuperCar = function() {
};
/**
* @return {obj.SuperCar}
*/
obj.SuperCar.create = function() {
return new obj.SuperCar();
}
Car.js
/**
* @param {*} prop1
* @constructor
* @extends {obj.SuperCar}
*/
obj.Car = function(prop1) {
goog.base(this);
this.prop1 = prop1;
};
goog.inherits(obj.Car, obj.SuperCar);
/**
* @param {*} prop1
* @return {obj.Car}
* @override
*/
obj.SuperCar.create = function(prop1) {
return new obj.Car(prop1);
}
Normally if you are going to override a function you can use /** @inheritDoc */
annotation or if you are going to change the function definition then write the new function definition and add the @override
annotation.
Upvotes: 1