Roopesh Shenoy
Roopesh Shenoy

Reputation: 3447

Dynamically defined Function Names In JavaScript?

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!

Upvotes: 1

Views: 749

Answers (2)

ndp
ndp

Reputation: 21996

Yeah, combinging jfriend's solutions works nicely:

function PartMetaData(dataElement) {
    this.metaData = new MetaData(dataElement);

    this.getProperty = function(name) {
        return this.metaData.getElement("data-part-" + name);
    }

    this.makeAccessor = function(name) {
       this["get" + name.substr(0,1).toUpperCase() + name.substr(1)] =
          function() { return this.getProperty(name); }
    }

    var items = ["description", "weight", "height", "width", "length"];
    for (var i = 0; i < items.length; i++) {
        this.makeAccessor(this, items[i]);
    }
}

Upvotes: 0

jfriend00
jfriend00

Reputation: 707308

You can make a helper function that you can use like this to create all those methods.

function PartMetaData(dataElement) {
    this.metaData = new MetaData(dataElement);

    function makeAccessor(o, itemName) {
        var ucase = itemName.substr(0,1).toUpperCase() + itemName.substr(1);
        o["get" + ucase] = function() {
            return this.metaData.getElement("data-part-" + itemName);
        }
    }

    var items = ["description", "weight", "height", "width", "length"];
    for (var i = 0; i < items.length; i++) {
        makeAccessor(this, items[i]);
    }
}

A simpler way of doing this with a slightly different interface would be this:

function PartMetaData(dataElement) {
    this.metaData = new MetaData(dataElement);
    this.getProperty = function(name) {
        return this.metaData.getElement("data-part-" + name);
    }
}

And, you would use it like this:

partMetaData.getProperty("height");

Upvotes: 3

Related Questions