John G Harris said the following on 5/28/2007 3:28 PM:
>> -Lost wrote on 27 mei 2007 in comp.lang.javascript:
>>
[quoted text clipped - 25 lines]
> The OP hasn't said that one, two, and three are *global* functions. We
> are just guessing that they are.
Considering that they asked how to execute that function though, it is
pretty reasonable to assume they are global functions.
> The OP hasn't said that the code is running in a browser, and so has a
> window variable. We are just guessing that it has.
The default, here - unless stated otherwise -, is that it is running in
a browser.
> The OP hasn't said if the code can run inside a 'with' statement and
> whether the function names are allowed to be captured by the 'with'
> object. We are just guessing that they aren't.
And whether it can or not is irrelevant.
> To make 'arr[0]' do exactly what writing 'one' would have done you need
> to use 'eval'.
I flat don't believe that. In fact, I know it isn't true.

Signature
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
John G Harris - 29 May 2007 21:32 GMT
>John G Harris said the following on 5/28/2007 3:28 PM:
<snip>
>> To make 'arr[0]' do exactly what writing 'one' would have done you need
>> to use 'eval'.
>
>I flat don't believe that. In fact, I know it isn't true.
The following code alerts 'Local!', 'Local!', 'Global!'. How do you
alter the window[... part to make it alert 'Local!', 'Local!', 'Local!'?
<SCRIPT type="text/javascript">
var arr = ['one', 'two', 'three'];
function one() { alert("Global!"); }
var x = new Object();
x.one = function() { alert("Local!"); }
with (x)
{
one();
eval( arr[0] + "()");
window[arr[0]]();
}
</SCRIPT>
Oh, I forgot to say : it's copy-and-paste code that knows nothing about
x, not even its name.
John

Signature
John Harris
Randy Webb - 31 May 2007 01:31 GMT
John G Harris said the following on 5/29/2007 4:32 PM:
>> John G Harris said the following on 5/28/2007 3:28 PM:
>
[quoted text clipped - 5 lines]
> The following code alerts 'Local!', 'Local!', 'Global!'. How do you
> alter the window[... part to make it alert 'Local!', 'Local!', 'Local!'?
First, I would alter it not to use "with"
<URL: http://www.javascripttoolbox.com/bestpractices/#with>
because of scope chain issues while using the "with" operator.
Aside from that, I would have to tinker with it. And I would probably
end up being inclined to believe it to be a legitimate use of eval and
add it to my list.
Interesting code to say the least.

Signature
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/