nicholaswmin
nicholaswmin

Reputation: 22949

Parameter as actual code substitution in Javascript

I would like to pass a parameter to a function that is used as an actual piece of code

this.illuminateLeg = function(whom) {
    var propertiesToIlluminate = [], prop, illuminateInternal, i = 0, delay = 100, intervalId;
    for (key in this.whom.zoom) {
        propertiesToIlluminate.push(this.whom.zoom[key]);
    }
}

I am trying to pass a whom parameter that is used to iterate over whom properties of my object.

I know its possible to pass strings as arguments, but how about actual pieces of code?

Is it possible to do so?

Upvotes: 0

Views: 33

Answers (2)

Platinum Azure
Platinum Azure

Reputation: 46183

If the value of whom is the name of a property, you can do this:

for (key in this[whom].zoom) {

Otherwise, you could allow callers to pass in a function which takes the object:

this.illuminateLeg = function(getWhom) {
    /* ... */

    for (key in getWhom(this).zoom) {
        /* ... */
    }

// Call with function as argument:
obj.illuminateLeg(function (param) {
    return param.somebody.somethingElse;
});

Upvotes: 1

Johnbabu Koppolu
Johnbabu Koppolu

Reputation: 3252

Something like this?

 for (key in this[whom].zoom)

Instead of

for (key in this.whom.zoom)

and then call the function like this

this.illuminateLeg("whom")

Upvotes: 2

Related Questions