javascript dereference?

A

a.iliadis

Hi,

I have the following code:

function abc(){
eval('function __newfunc(){alert("hello");}');
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.
In firefox and opera all I had to do is change eval to window.eval to
force it to
execute in the window context. Unfortunetly that does not work with IE.
However
I could just do the following: window.__newfunc = __newfunc. and force
it to register.
My question is how can I do that without knowing the function name of
the one being
defined inside the eval expression, in this case __newfunc().

Thanks
 
L

Lasse Reichstein Nielsen

function abc(){
eval('function __newfunc(){alert("hello");}');

Which could just as well be:
function __newfunc(){alert("hello");}
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.

As it should.
In firefox and opera all I had to do is change eval to window.eval
to force it to execute in the window context. Unfortunetly that does
not work with IE.

It's not guaranteed to work like that, or at all. It's a proprietary
feature of those browsers that is not matched by other browsers. That
happens all the time, usually in the opposite direction :)
However I could just do the following: window.__newfunc =
__newfunc. and force it to register.

That's just assigning to a property of the global object, which makes
the assigned value available as a global variable.
My question is how can I do that without knowing the function name
of the one being defined inside the eval expression, in this case
__newfunc().

First of all, why do you want to use eval? Is the function declaration
provided by the user or in some other way not known when the abc
function was written?

Assuming the function comes as a string (bad choice, but it happens):

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
}

/L
 
M

Martin Honnen

Lasse Reichstein Nielsen wrote:

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor does
the script engine used in Opera 8 or 9.
 
A

a.iliadis

Lasse said:
function abc(){
eval('function __newfunc(){alert("hello");}');

Which could just as well be:
function __newfunc(){alert("hello");}
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.

As it should.
In firefox and opera all I had to do is change eval to window.eval
to force it to execute in the window context. Unfortunetly that does
not work with IE.

It's not guaranteed to work like that, or at all. It's a proprietary
feature of those browsers that is not matched by other browsers. That
happens all the time, usually in the opposite direction :)
However I could just do the following: window.__newfunc =
__newfunc. and force it to register.

That's just assigning to a property of the global object, which makes
the assigned value available as a global variable.
My question is how can I do that without knowing the function name
of the one being defined inside the eval expression, in this case
__newfunc().

First of all, why do you want to use eval? Is the function declaration
provided by the user or in some other way not known when the abc
function was written?

Assuming the function comes as a string (bad choice, but it happens):

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
}

Unfortunetly none of the two is the case. Infact I dont know what the
javascript code would be. It may define from 1-n number of functions.
Also I cant know before hand what the function name would be.
I am trying to find a way to execute the javascript code without having
to do something like:
 
L

Lasse Reichstein Nielsen

Martin Honnen said:
Lasse Reichstein Nielsen wrote:

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor
does the script engine used in Opera 8 or 9.

True. And I thought I checked in Opera, but it must have been Firefox.

Well, in that case, one will have to pick the name out of the string:

function abc(funcString) {
var f = eval("("+funcString+")");
var nameMatch = /function\s+([\w$]+)\s*\(/.exec(funcString);
if (nameMatch) {
window[nameMatch[1]] = f;
}
}

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 5 Aug 2006 14:02:14 remote, seen in
news:comp.lang.javascript said:
Lasse Reichstein Nielsen wrote:

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor does
the script engine used in Opera 8 or 9.

Let the function be F : will the second word in F.toString() always be
F, the 'name' of the function?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top