473,594 Members | 2,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1028
PJ6 wrote on 09 dec 2005 in comp.lang.javas cript:
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******** ************@19 4.109.133.242.. .
PJ6 wrote on 09 dec 2005 in comp.lang.javas cript:
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.javas cript:
[...] 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
2009
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 instantiation of foo:
3
1463
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: Component *C = new Component(); But how to do with an array? I use: Component **C = new *Component(); But it's not right. If you can give me any information, I'll be so appreciated. Nikki
3
4320
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 like below: //GridView gv; BoundField gg_typ = new BoundField(); gg_id.DataField = "wm_id"; gg_id.HeaderText = "wm_id";
4
9479
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
1568
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 : 3) Now It calls one of the functions in generated code. // need help on this. I need guidance in third point.
6
1963
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 SomeClass(const SomeClass&); // disallow copy construction SomeClass& operator=(const SomeClass&); // disallow assignment public:
43
3789
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 "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
3
5554
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
1866
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 associate the call with the handling routine (for reference purposes, I'm calling the Google Language Translation API). As a work-around, I thought I'd dynamically generate a unique callback function for each API call. Right now, I'm stuck hobbling...
0
7947
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7880
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8255
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.