Hello,
I'm working currently on following snippet of code:
/****************************************************************
* UserInterface
****************************************************************/
function UserInterface() {
this.editability = 0;
this.filtrability = 0;
this.userInterfaceType = "";
}
UserInterface.prototype.setEditability = function(intValue) {
this.editability = intValue;
};
UserInterface.prototype.getEditability = function() {
return this.editability;
};
UserInterface.prototype.setFiltrability = function(intValue) {
this.filtrability = intValue;
};
UserInterface.prototype.getFiltrability = function() {
return this.filtrability;
};
UserInterface.prototype.setUserInterfaceType = function(strValue) {
this.userInterfaceType = strValue;
if (this.userInterfaceType === "inputField")
UserInterfaceInputField.call(this);
else if (this.userInterfaceType === "textArea")
UserInterfaceTextArea.call(this);
};
/****************************************************************
* UserInterfaceInputField
****************************************************************/
UserInterfaceInputField.prototype = new UserInterface();
UserInterfaceInputField.prototype.constructor = UserInterfaceInputField;
function UserInterfaceInputField() {
this.fieldLength = 20;
}
UserInterfaceInputField.prototype.setFieldLength = function(intValue) {
this.fieldLength = intValue;
};
UserInterfaceInputField.prototype.getFieldLength = function() {
return this.fieldLength;
};
/****************************************************************
* UserInterfaceTextArea
****************************************************************/
UserInterfaceTextArea.prototype = new UserInterface();
UserInterfaceTextArea.prototype.constructor = UserInterfaceTextArea;
function UserInterfaceTextArea() {
this.areaLength = 20;
this.areaHeight = 2;
}
UserInterfaceTextArea.prototype.setAreaLength = function(intValue) {
this.areaLength = intValue;
};
UserInterfaceTextArea.prototype.getAreaLength = function() {
return this.areaLength;
};
UserInterfaceTextArea.prototype.setAreaHeight = function(intValue) {
this.areaHeight = intValue;
};
UserInterfaceTextArea.prototype.getAreaHeight = function() {
return this.areaHeight;
};
The aim is to be able to use the invocation as in the code below:
var ui = new UserInterface();
ui.setEditability(1);
ui.setFiltrability(1);
ui.setUserInterfaceType("inputField");
setFieldLength(50); //Error "ui.setFieldLength is not a function"
alert(ui.getFieldLength()); //Error "ui.getFieldLength is not a function"
I know that I could simply add additional property and create an property object like here:
UserInterface.prototype.setUserInterfaceType = function(strValue) {
this.userInterfaceType = strValue;
if (this.userInterfaceType === "inputField")
this.interface = new UserInterfaceInputField();
else if (this.userInterfaceType === "textArea")
this.interface = new UserInterfaceTextArea();
};
but I don't want then to refer to subclasses' methods and properties through additional property (UserInterface.interface).
My current solution is to call a given subclass with a scope of parent class with a help of "switching" method UserInterface.setUserInterfaceType(). As I have checked the subclass' constructor is indeed invoked, but the ui var in the code still refers to the class UserInterface..
What am I missing and how to achieve my goal?
Thank you for help!