473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 1020
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
3
by: Nikki | last post by:
Hi, I have a question when I am programming. I define a class (Component{};) and I want to use "new" to dynamiclly alloct a class array, such as Component C. I know how to do with one element:...
3
by: lupina | last post by:
Hi, I need to add controls to the TemplateField of GridView dynamiclly at runtime (beacuse my GridView handle faw different DataSets, so I cannot do it in aspx file). The BoundFields I create...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
10
by: shaanxxx | last post by:
I want to write a programme. I explain what it does. 1) It generates C code . // i know how to do it 2) It compiles that generated code. // i know how to do it. IMPORTANT ONE :...
6
by: JohnQ | last post by:
I like, non-copyable, non-assignable and, most often, non-default-constructable also, as a starting point for class design: class SomeClass { SomeClass(); // disallow default construction...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
3
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, Using ASP .Net, I'm trying to create a ImageButton when the user clicks on a button, this is my code: protected void logout_btn_Click(object sender, ImageClickEventArgs e) {
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.