473,394 Members | 1,869 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,394 software developers and data experts.

(new Function) as function.

How can I create a function nF such that for example
var sum = nF('a','b','return a+b');
and
var sum = new Function('a','b','return a+b');
do the same?

function nF(){
return new Function(arguments);
} // won't work. because Function expects first argument to be a
string, not an agruments list.

function nF(){
return (new Function).apply(null,arguments);
} // won't work. (new Function) is not a function.

var nF = Function; // wont' work.

Jul 10 '06 #1
3 1742
HopfZ wrote:
How can I create a function nF such that for example
var sum = nF('a','b','return a+b');
and
var sum = new Function('a','b','return a+b');
do the same?
function fn(args, body) {
return new Function(args, body);
}

var f = fn(['a','b'],'return a+b');
alert(f(1,2,3));
JW
Jul 10 '06 #2
Janwillem Borleffs wrote:
HopfZ wrote:
How can I create a function nF such that for example
var sum = nF('a','b','return a+b');
and
var sum = new Function('a','b','return a+b');
do the same?

function fn(args, body) {
return new Function(args, body);
}

var f = fn(['a','b'],'return a+b');
alert(f(1,2,3));
JW
Thanks.

I wanted to make functions R and D that work as below.

/* t.
R('a_+b_*c_') returns function(a_,b_,c_){return (a_+b_*c_);}
R('x,y,z->(x+y)/z') returns function(x,y,z){return ((x+y)/z);}
D('if(b_) print(a_)') returns function(a_,b_){if(b_) print(a_)};
D('x,y->if(y) print(x)') returns function(x,y){if(y) print(x)};
R meaning Return, and D meaning Do, a_,b_,c_ meaning first, second and
third argument.
*/
function D(s){
if(/[abc]\_/.test(s)){
return new Function(['a_','b_','c_'],s);
}
var t = s.split('->');
return new Function(t[0].split(','),t[1]);
}

function R(s){
if(/[abc]\_/.test(s)){
return new Function(['a_','b_','c_'],'return ('+s+');');
}
var t = s.split('->');
return new Function(t[0].split(','),'return ('+t[1]+');');
}

I was stuck because I didn't know that (new Function('a','b','return
a+b')) can be alternatively written as (new Function(['a','b'],'return
a+b')).

Jul 11 '06 #3
"HopfZ" <ho******@gmail.comwrites:
I was stuck because I didn't know that (new Function('a','b','return
a+b')) can be alternatively written as (new Function(['a','b'],'return
a+b')).
That's a coincidence, actually.
What you can write is
new Function("a,b","return a+b;")
Passing an array as first parameter just means that it is implicitly
converted to a string, which gives the same effect as joining with ",".
I.e.,
new Function(["a","b"],"return a+b;")
is equivalent to
new Function(["a","b"].join(","),"return a+b;")
which is againg the same as the first function above.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 11 '06 #4

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

Similar topics

4
by: Madestro | last post by:
Hi guys, I am making a small program to retrieve e-mails from POP accounts. I got all the e-mail parsing stuff figured out, but I cannot seem to come up with a way to find out which e-mails are...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
3
by: Lauren Quantrell | last post by:
Maybe a dumb question - I'm new to ODBC. How do I install an Access ..mde file on a user's workstation and create the ODBC connection to the backend SQL Server database without having to go through...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
14
by: emailscotta | last post by:
Some of the object properties in the Dojo Toolkit are set to objects but they are using syntax like this: object.property = new function() { this.property = someValue; this.property =...
4
by: Jerry West | last post by:
I am new to VB .NET moving from VB6. I wrote the following code and as a result I received an error concerning it from the IDE. I don't understand why I get the message or why that what I am doing...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
1
by: abhishekbrave | last post by:
The code below is opening a calendar on mouse over in the same window. I need the calendar to be opened in new window. Have to fulfill this requirement urgentely so posting the whole code here. I...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
0
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...
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...

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.