473,659 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating functions dynamically

Hello,

Given an expression f = '2*x+3' from a user input
I would like to do some sort of

function userFunction(x) :
return f(x)

is it possible with Javascript ?
Thanks for your advises

Regards

Salvatore

Jul 20 '05 #1
2 2479


Salvatore wrote:
Hello,

Given an expression f = '2*x+3' from a user input
I would like to do some sort of

function userFunction(x) :
return f(x)

is it possible with Javascript ?


var fun = new Function("x", f)

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Salvatore <ar******@wanad oo.fr> writes:
Given an expression f = '2*x+3' from a user input
I would like to do some sort of

function userFunction(x) :
return f(x)

is it possible with Javascript ?


This is one of the (very few) cases where you might want to
use the eval function (or the comparable Function constructor).

Just remember that the eval/Function functions understand all
javascript, not just arithmetic expressions, so there is plenty
of rope for a user to hang himself with.

Try this:

var userFunction = Function("x","r eturn ("+f+");");

(by wrapping the value of "f" in "return (...)", you guarantee
that "f" contains an expression, not a full Javascript program.
It doesn't change anything for a determined hacker who wants
to execute arbitrary code, but it catches some accidental
mistypings. Remember: *never* use eval for server-side code!)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3188
by: Germic | last post by:
Hi, How can we dynamically create a wrapper for an webservice? Example : I write an client application in C# that consumes a webservice, I need that application to read the URL of the WSDL file from an .INI file and then compile and create wrappers for it. (So incase one webservice is down then i could point my application to another URL) Of course, the functions THAT ARE CONSUMED by the client application would
2
4560
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
6
1914
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create a new thread to display a customer on the screen for example. However, I have a problem with circular references in my objects which means that I have to load the customer object using reflection ie :
6
6091
by: Tex | last post by:
I am writting a survey system web application. I am using ASP.Net 2, C# and MS SQL 2005. I am able to store surveys and questions associated to the surveys just fine. The problem I am having is Dynamically creating a survey on a page. I have read some MS articles on dynamic creation of web forms and have been able to create one question on a page. I am not sure how to create a variable amount of different types(radio button lists,...
9
2064
by: William | last post by:
Heya, I have been looking all over Google for ways to *create* an XML document using any Javascript object, and it seems that no one has ever tried or ever needed this. I want to send the server some info dynamically created on the page, and instead of sending a complicated GET request with the info, or creating an XML using my own set of functions, is there any object that allows you to use the Document Object Model, like adding...
1
2480
by: joe_doufu | last post by:
I'm creating a page with multiple "widgets", each with its own XMLHttpRequest object, so the user can play with the widgets in parallel. The widgets are enclosed in divs with class "widget" and a unique ID which tells me the name of the PHP file for the widget. Each widget should be able to access its PHP file with GET and POST methods, and to re-draw its innerHTML on response. I am using a simple Ajax library called XHConn...
4
2964
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a while and it seems it's not possible because the row that gets added with each click won't get recreated after a post back. After all the reading it seems that any dynamically created controls must be created in the Init event to be recreated after...
3
1860
by: Jim Horvath | last post by:
I know how to create exe applications in dotnet that use functions supplied by pre-existing dlls. How do you turn that around? I want to create an exe program that will be the pre-existing piece. It's functionality can then be extended (using new dlls) without re-compiling the exe. My proposed program is a test sequencer for an industrial control application. I want the exe to handle common housekeeping functions (stepping through...
11
2566
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other modules, but for some reason it only works using the "import <mod>" syntax, and not "from <modimport *" syntax... i.e. in the latter case, the function is dynamically generated, but not accessible from the importing module. Any ideas on what I can...
0
8428
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
8341
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
8751
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...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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
7360
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...
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.