0

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?

2
  • Your question is really unclear. Are you trying to access property of this, but you want to get property name from whom argument? Then use square bracket syntax, as suggested in answer below. Or you want to pass function ("piece of code") as argument? Then just pass it. Commented Jul 5, 2014 at 16:42
  • Got it, the answer below hits the spot. Commented Jul 5, 2014 at 16:44

2 Answers 2

2

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")
Sign up to request clarification or add additional context in comments.

Comments

1

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;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.