Reputation: 1456
In Javascript, is there a way to create a function from a string (such as through the new Function() constructor) and have it inherit the parent scope? For example:
(function(){
function yay(){
}
var blah = "super yay"
yay.prototype.testy = new Function("alert(blah)")
yay.prototype.hello = function(){alert(blah)}
whee = new yay();
whee.hello()
whee.testy()
})()
Is there any way to make whee.testy() also alert "super yay"?
Upvotes: 5
Views: 2278
Reputation: 19344
This should give you what you want...
var inputAction = "alert(blah)"; yay.prototype.testy = eval("(function(){ " + inputAction + "; })")
It basically wraps your intended action inside an anonymous function, this gets fully evaluated on the eval, but isn't run right away, it's wrapped into a function.
You can go a little farther here, but without knowing what you want to accomplish it's hard to say.
Upvotes: 1
Reputation: 95424
The reason why whee.testy
doesn't work is because declaring a function using new Function("alert(blah)")
creates the function outside the current closure. Since blah
is defined inside your closure, you do not have access to it, and it throws an undefined error.
Here is a proof of concept example:
var blah = "global (window) scope";
(function(){
function yay(){
}
var blah = "closure scope";
yay.prototype.testy = new Function("alert(blah)");
yay.prototype.hello = function(){ alert(blah); }
whee = new yay();
whee.hello();
whee.testy();
})();
Upvotes: 0
Reputation: 1614
You don't need eval.
You have to concatenate the blah to the string function but JavaScript will complain that there is no ";" just before the concatenation this is because blah is just some text when you concatenate it. You have to escape two "\"" around the variable blah in order to make it look like a string with text in it.
(function(){
function yay(){
}
var blah = "super yay"
yay.prototype.testy = new Function("alert(\""+blah+"\")")
yay.prototype.hello = function(){alert(blah)}
whee = new yay();
whee.hello()
whee.testy()
})()
This will alert "super yay" two times!
Upvotes: -1
Reputation: 1456
(function(){
function yay(){
}
var blah = "super yay"
yay.prototype.testy = eval("(function(){alert(blah)})")//new Function("alert(blah)")
yay.prototype.hello = function(){alert(blah)}
whee = new yay();
whee.hello()
whee.testy()
})()
This seems to work for me, and none of the eval'd data is from any untrusted source. It's just to be used for minifying code.
Upvotes: 1
Reputation: 183
You mean eval?
(function(){
function yay(){
}
var blah = "super yay"
yay.prototype.testy = new Function(eval("alert(blah)")) // <---------------
yay.prototype.hello = function(){alert(blah)}
whee = new yay();
whee.hello()
whee.testy()
})()
However there are two morally objectionable feature of JS here. 1) eval is "evil", and 2) the code inside the eval can see the variables outside.
Upvotes: -1
Reputation: 117579
Actually, combining function
and eval
should do what you want:
// blah exists inside the 'hello' function
yay.prototype.hello = function(){ alert(blah) }
// blah also exists inside the 'testy' function, and
// is therefore accessible to eval().
yay.prototype.testy = function(){ eval('alert(blah)') }
Upvotes: 1