This is what I'm trying to achieve. Here is an existing piece of code -
function PartMetaData(dataElement) {
this.metaData = new MetaData(dataElement);
this.getDescription = function () {
return this.metaData.getElement("data-part-description");
};
this.getLength = function () {
return this.metaData.getElement("data-part-length");
};
this.getWidth = function () {
return this.metaData.getElement("data-part-width");
};
this.getHeight = function () {
return this.metaData.getElement("data-part-height");
};
this.getWeight = function () {
return this.metaData.getElement("data-part-weight");
};
}
Now look at the relationship between the functionname and the attribute name - this looks a lot like repetitive code that can be avoided. Is that something that can be achieved? Is there a way to allow "any" function call to an object and determine what to do depending on the function name?
for eg something like (the syntax is cooked up, I don't know if something like this exists in Javascript, or any other language for that matter)
function PartMetaData(dataElement){
this.metaData = new MetaData(dataElement);
this.get(name?) = function () {
return this.metaData.getElement("data-part-" + name.toLower());
}
such that the caller can still call
partMetaData.getDescription();
Any ideas? I know I can change the signature and make it one function, I just want to know if the signature can be kept as is with some JS (black?) magic.. Thanks!