469,306 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

dynamiclly generated functions?

PJ6
Maybe this is a whacky question, but is it possible, with Javascript, to
dynamically generate new functions - that is, execute the text contents of a
variable?

Paul
Dec 9 '05 #1
3 969
PJ6 wrote on 09 dec 2005 in comp.lang.javascript:
Maybe this is a whacky question, but is it possible, with Javascript,
to dynamically generate new functions - that is, execute the text
contents of a variable?


Did you try this evil thing:

a = "alert('yes')"
eval(a)

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Dec 9 '05 #2
PJ6
Ooooooh.

Mmmm!!!

Thanks :) :) :)
Paul

"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.242...
PJ6 wrote on 09 dec 2005 in comp.lang.javascript:
Maybe this is a whacky question, but is it possible, with Javascript,
to dynamically generate new functions - that is, execute the text
contents of a variable?


Did you try this evil thing:

a = "alert('yes')"
eval(a)

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Dec 9 '05 #3
On 09/12/2005 15:20, Evertjan. wrote:
PJ6 wrote on 09 dec 2005 in comp.lang.javascript:
[...] is it possible, with Javascript, to dynamically generate new
functions - that is, execute the text contents of a variable?

[snip]
a = "alert('yes')"
eval(a)


The eval function is an option, though if the OP really does want to
create functions, then the Function constructor would be more appropriate.

For example,

var myFunction = new Function('a', 'z', 'return a + z;');

or:

var myFunction = new Function('a,z', 'return a + z;');

is roughly the equivalent of:

function myFunction(a, z) {
return a + z;
}

The most significant difference between the first two and the third, is
that function declarations (the third) and expressions (shown below)
have their scope chain determined with respect to where they are
defined. Function objects created with the Function constructor only
ever have the global object in their scope chain. The following
demonstrates this:

function outer1(arg) {
return function() {
alert(arg);
};
}

function outer2(arg) {
return new Function('alert(arg);');
}

outer1('Hello world')(); /* Displays 'Hello world' */
outer2('Foo bar baz')(); /* Fatal error: arg is not defined */

The two outer* functions create and return function objects. The first
function will have the formal argument, arg, in its scope chain so it
may display its value. The second function does not and, because there
is no global variable named 'arg', an error will occur.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nikki | last post: by
3 posts views Thread by lupina | last post: by
4 posts views Thread by moleskyca1 | last post: by
10 posts views Thread by shaanxxx | last post: by
3 posts views Thread by =?Utf-8?B?R2lkaQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.