Let's start from the code:
function say(name) {
var ghost=function () {
function ghost() {
alert('!');
};
return body;
};
eval("var body=''+"+name+';');
eval(name+('=('+ghost).replace('body', body)+')();');
eval(name+'();');
}
function Baal() {
if ('undefined'===typeof ghost) {
say('Baal');
return;
}
ghost();
}
say('Baal'); // or just Baal();
Looks like that saying the devil's name invoke his presence (well, maybe he needs somebody for spiritual possession) ..
As you can see the ghost doesn't exist along with Baal
, but we can invoke it since there're eval
s in say(name)
.
say(name)
reassigns Baal
to its code body as a closure and makes it captured a ghost
method, that's how things work. But I'm trying to avoid eval
..
So .. let me reword the question:
How do I make a nonexistent(and not a member or global) method invocable without using eval
?