2

I have a set of variables that I'd like to take as input to a function, then define them for use within that function.

For example:

var variables = {
    foo: 1,
    bar: 2,
};

function doSomething (scope) {
    makeLocal(scope); // I'd like something to do this

    return foo + bar;
}

doSomething(variables); // should return 3

Does such a thing exist? PHP has a facility to do this called extract, I'm hoping a similar thing exists for JavaScript. I know that I can add attributes to window, but I'd prefer not to pollute my global namespace if possible.

3
  • 4
    What's wrong with scope.foo + scope.bar? Commented Jul 2, 2014 at 16:11
  • @entropic I plan for there to be a lot of variables, and for them to be used a lot. I'd rather not have "scope" (or even a one-character variable name) everywhere if I can help it Commented Jul 2, 2014 at 16:14
  • 2
    There's not a native metod for this in JS. If this could be done using some ugly eval() trick, you'd have to do it in doSomething(), otherwise new variables would be in the scope of makeLocal(). Commented Jul 2, 2014 at 16:19

2 Answers 2

2

JavaScript doesn't have any equivalant of PHP's extract. One way to get something close would be to bind the current context when calling makeLocal() and assign the variables to that:

Demo

var variables = {
    foo: 1,
    bar: 2,
};

function makeLocal(scope){
    for(var key in scope){
        this[key] = scope[key];   
    }
}

function doSomething (scope) {
    makeLocal.call(this, scope);

    return foo + bar;
}

The downside to this is if doSomething() has no context, it will be the global object (window in browsers) and your variables would pollute the global scope.

Sign up to request clarification or add additional context in comments.

3 Comments

I may have spoken too soon, this does not appear to work in any context other than window. Can you provide an example of it working in another context?
This will only work when this is the global context (window most of the time, except e.g. in strict mode). Will not work when doSomething.call({}, scope) or obj.doSomething(scope) for that matter; you'd need to use return this.foo + this.bar;. But when it works, it will pollute the global namespace, of course.
Yeah agreed - it doesn't work outside the global scope. The only way to get it to work in any scope would be to make it store window[key] = scope[key] instead of this, but that's always using globals. I don't believe there is any other way to do this in JavaScript.
0

There is a good javascript library that allow you to use php functions, just like extract.

Have a look !

http://phpjs.org

Here is what you're looking for:

http://phpjs.org/functions/extract/

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.