Reputation: 2897
I am trying to define a function which can access variables which are in the scope of the function that is calling it.
( I am attempting to build a string formatter that is prettier than "a" + b, and more terse than String.format("a{0}",b). So I can get SF("a{b}"), and I don't know if it is possible )
so
function magic(str) { return parentScope[str]; // or return eval( str ); // or something like that } function call() { var a = 1; alert( magic( "a" ) ); } call();
would alert "1".
currently I can do this by having the function return code to be eval'd, but it seems like there must be a better way.
Upvotes: 4
Views: 2683
Reputation: 75872
As far as I know it is impossible to reach out and grab an arbitrary variable from a function in javascript. From an object yes, but not from an executing method.
I can't think of any function method (apply, call, otherwise) which helps you here: frankly I think you're trying too hard for a marginal benefit over the normal string.format, er... format.
Upvotes: 0
Reputation: 3861
Just put your 'magic' function into the call function:
function call() {
function magic(str) {
return a;
}
var a = 1;
alert( magic() );
}
call();
This mechanism behind the scenes is called closures and which are very powerful concept. Just think of an AJAX request where the callback function still can access the local variables of the calling function.
UPDATE
I misunderstood your question. The thing you actually want to build is impossible to implement in JavaScript because the is no way to access the caller's scope.
UPDATE 2
Here is my string formatting function. Not as concise as you may want it, but still very usable.
String.prototype.arg = function()
{
result = this;
// This depends on mootools, a normal for loop would do the job, too
$A(arguments).each((function(arg)
{
result = result.replace("%", arg);
}).bind(this));
return result;
}
You can use it like:
"%-% %".arg(1,2).arg(3)
and receive
"1-2 3"
Upvotes: 2