473,804 Members | 3,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new Function(...) abuse in FAQ?

Hi,

Why does http://www.jibbering.com/faq/ uses new Function constructor
instead of function expressions (function(...) { ... }) when defining
new functions? E.g. for LTrim and toFixed. Is the reason that you want
to avoid accidental closures? Or does some obscure browser does not
support function expressions?

This just looks weird, given that http://www.jibbering.com/faq/#FAQ4_40
says to only use eval() (which is not really much different from new
Function()) only for evaluating code that only becomes known at
run-time.

Nickolay

Jan 22 '07
14 1562
Richard Cornford wrote:
It is not necessarily possible to regard any simple example as good or
bad. You have concerned yourself with the explicit declaration of
variables (in a nearby thread), but what is to stop someone seeing an
example of such and making a bad application of a declaration such as
features in the second example of code in what follows?
Right. As you noticed, I think that using Function constructor should
be avoided, unless using it has benefits. This is why I called the
examples in the FAQ "bad". Apparently, you and others who have a say
over what is in the FAQ disagree. Oh well.
We even have browser's like Mozilla/Gecko spitting out spurious and bogus
"warnings"
Any examples? (I know this is off-topic, but since I'm a bit familiar
with moz and never heard of these, you got me interested)

Given the code:-

if(window.clipb oardData){
alert('clipboar d')
}

Mozilla 1.6 (for no other reason than it is the Mozilla version on this
machine) outputs the warning:-

"Warning: reference to undefined property window.clipboar dData"
It seems this no longer happens in newer versions. (1.6 is three years
old.)
Given the code:-

function testFunc(param) {
var param; // This is a "bad" application of the otherwise "good"
// declaration of local variables.
}

Mozilla outputs the warning:-

"Warning: variable param hides argument"

- and that is an absolute lie. ECMA 262 is very clear about how such
code should be handled (Section 10.1.3) and not only does the - var
param; - not "hide" the - param - formal parameter it is actually
specified as doing nothing
Right, that's how it works, and the warning is indeed a bit misleading.
Thanks for mentioning it.
>Why does http://www.jibbering.com/faq/ uses new Function
constructor instead of function expressions (function(...) { ... })
when defining new functions?

Mostly because much of it was written half a decade ago when anything else
would not have been cross-browser. It has remained because it a. it still
works fine, b. no compelling argument has ever been presented for changing
it (leaving it largely a matter of taste as to whether it gets changed).

Are you re-ordering the material you are quoting (without stating as
much)? That would generally be regarded as disingenuous.
Am I? Sorry if I did. I do have the habit of snipping large portions of
text, but usually don't reorder it.
You could save me and yourself some time by only posting this. You
answered my question.
<snip>

I was interested in whether you would present any compelling arguments
for changing the FAQ. You have not.
There are no compelling arguments for changing it if you don't think
that putting code in string literals for no reason is wrong. I hoped
somebody would tell me the reason for *not* changing it. Apparently
people here just like the Function constructor more.

(You probably noticed, as did guy who called me a jerk in the other
thread, that I got off on the wrong foot the day I first replied. Sorry
if I offended you, you're a nice and helpful fellow. Thanks for your
replies.)

Nickolay

Jan 23 '07 #11
Nickolay Ponomarev said the following on 1/23/2007 2:55 PM:
Richard Cornford wrote:
>It is not necessarily possible to regard any simple example as good or
bad. You have concerned yourself with the explicit declaration of
variables (in a nearby thread), but what is to stop someone seeing an
example of such and making a bad application of a declaration such as
features in the second example of code in what follows?
Right. As you noticed, I think that using Function constructor should
be avoided, unless using it has benefits. This is why I called the
examples in the FAQ "bad". Apparently, you and others who have a say
over what is in the FAQ disagree. Oh well.
For a very long time a lot of people complained about what was in the
FAQ but never proposed an alternative. If you have alternative code that
accomplishes the same goal without drawbacks then post it. If it doesn't
get whammied to death then it can be changed in the FAQ. But to simply
say "I think that's wrong/bad" without offering an alternative is a
quick route to it not getting changed. But, if it comes down to Richard
saying "The code is fine" then I will give you 100-1 that it will stay
the way it is. If you post alternative code and Richard doesn't disagree
with it then you get another 100-1 that it will get changed.

<snip>
>Given the code:-

function testFunc(param) {
var param; // This is a "bad" application of the otherwise "good"
// declaration of local variables.
}

Mozilla outputs the warning:-

"Warning: variable param hides argument"

- and that is an absolute lie. ECMA 262 is very clear about how such
code should be handled (Section 10.1.3) and not only does the - var
param; - not "hide" the - param - formal parameter it is actually
specified as doing nothing

Right, that's how it works, and the warning is indeed a bit misleading.
I wouldn't call an "absolute lie" a "bit misleading". It is 100% totally
misleading and blatantly wrong.

<snip>
>I was interested in whether you would present any compelling arguments
for changing the FAQ. You have not.
There are no compelling arguments for changing it if you don't think
that putting code in string literals for no reason is wrong. I hoped
somebody would tell me the reason for *not* changing it. Apparently
people here just like the Function constructor more.
He explained why it was written the way it was written. If browsers are
to the point where they can handle a different construct then fine.
(You probably noticed, as did guy who called me a jerk in the other
thread, that I got off on the wrong foot the day I first replied. Sorry
if I offended you, you're a nice and helpful fellow. Thanks for your
replies.)
Nothing in Usenet is worth getting offended over. By tomorrow, there
will be another 40 or so posts to move on to. But, as for being a jerk,
welcome to the jerk club, I have been called a lot worse than that :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 24 '07 #12
On Jan 24, 3:27 am, Randy Webb <HikksNotAtH... @aol.comwrote:
Nickolay Ponomarev said the following on 1/23/2007 2:55 PM:
Richard Cornford wrote:
It is not necessarily possible to regard any simple example as good or
bad. You have concerned yourself with the explicit declaration of
variables (in a nearby thread), but what is to stop someone seeing an
example of such and making a bad application of a declaration such as
features in the second example of code in what follows?
Right. As you noticed, I think that using Function constructor should
be avoided, unless using it has benefits. This is why I called the
examples in the FAQ "bad". Apparently, you and others who have a say
over what is in the FAQ disagree. Oh well.For a very long time a lot of people complained about what was in the
FAQ but never proposed an alternative. If you have alternative code that
accomplishes the same goal without drawbacks then post it. If it doesn't
get whammied to death then it can be changed in the FAQ. But to simply
say "I think that's wrong/bad" without offering an alternative is a
quick route to it not getting changed. But, if it comes down to Richard
saying "The code is fine" then I will give you 100-1 that it will stay
the way it is. If you post alternative code and Richard doesn't disagree
with it then you get another 100-1 that it will get changed.
I thought the alternative was obvious, so I didn't mention it. If it
wasn't obvious, here it is:
String.prototyp e.LTrim = function(s) { return this.replace(/^\s+/,'');
}

The browsers/JS engines I care about can handle this, but I'm sure
somebody will come up with a reason this is totally wrong and shouldn't
be in the FAQ. I don't care too much though, and It's not very
important.
<snip>
Given the code:-
function testFunc(param) {
var param; // This is a "bad" application of the otherwise "good"
// declaration of local variables.
}
Mozilla outputs the warning:-
"Warning: variable param hides argument"
- and that is an absolute lie. ECMA 262 is very clear about how such
code should be handled (Section 10.1.3) and not only does the - var
param; - not "hide" the - param - formal parameter it is actually
specified as doing nothing
Right, that's how it works, and the warning is indeed a bit misleading.I wouldn't call an "absolute lie" a "bit misleading". It is 100% totally
misleading and blatantly wrong.
The point of the warning is that you have a |var|-declared variable and
a parameter with the name, and it gets the point across.

BTW, this will be fixed for Firefox 3 and other Gecko 1.9-based apps,
thanks to Richard's notice.
<snip>
Nickolay

Jan 24 '07 #13
Nickolay Ponomarev wrote:
Richard Cornford wrote:
>It is not necessarily possible to regard any simple example as
good or bad. You have concerned yourself with the explicit
declaration of variables (in a nearby thread), but what is to
stop someone seeing an example of such and making a bad
application of a declaration such as features in the second
example of code in what follows?
Right. As you noticed, I think that using Function constructor
should be avoided, unless using it has benefits.
Which is a reasonable position, but not necessarily better justified
than a position that says that the use of the Function constructor
should only be avoided when its use does no harm. A distinction further
blurred by the realisation that the decision is not made on a single
axis, but instead in a space where many factors influence what may be
regarded as 'benefit' and 'harm', some of which are context related.
This is why I called the examples in the FAQ "bad".
We have seen the specifics: the maintenance headaches and debugging
problems that follow form indirect scripting. Both are valid and
realistic concerns, but more significant with an increase in the size and
complexity of the indirect scripting used. Thus the relative simplicity
of the examples in the FAQ may be argued to mitigate those concerns in
the specific case, and the "bad example" then follows from people seeing
how the Function constructor can be used and extending that to
realisation of what may be done with them, while perhaps not thinking of
undesirable consequences that follow from the elaboration of the example.

It is almost certainly true that if the example in the FAQ were created
today it would be written to assign the results of function expressions
instead of functions constructed with the constructor. However, the
result may still be validly subject to the accusation that it was a "bad
example" because an individual seeing such an example might also see how
the assigning of function expressions could be applied in other contexts
(such as assigned directly to event handlers) and so elaborate the
example into a script that leaked memory in IE browsers (possibly not
noticing at first and incorporating the technique to such an extent that
when the problem was exposed the effort needed to mitigate it had become
a real maintenance headache).
Apparently, you and others who have a say
over what is in the FAQ disagree. Oh well.
The FAQ doesn't really work like that. If there was a sufficient
expression of opinion (from those who's opinions are worth listening to
(so not VK for obvious reasons)) in favour of a change (or a really
compelling argument) then a change would likely follow. (for example, I
have never been in favour of including David Flanagan's book(s) in the
FAQ, yet I added the entry based upon the weight of opinion expressed in
the group.

To the best of my recollection Yep and Evertjan (among some less
significant others) have expresses the opinion that the FAQ should change
from using the Function constructor to using function expressions. You
have added your voice and it probably would not take many more expressing
a similar opinion to qualify as sufficient.

From my point of view it could still go either way, which is shy I am
interested in the possible existence of a compelling argument for one
position or the other. Though I would not like to see the FAQ deprived of
all examples of the use of the Function constructor (this it, I don't
think its use is inherently bad).
>>>We even have browser's like Mozilla/Gecko spitting out
spurious and bogus "warnings"

Any examples? (I know this is off-topic, but since I'm a bit
familiar
with moz and never heard of these, you got me interested)

Given the code:-

if(window.clip boardData){
alert('clipboar d')
}

Mozilla 1.6 (for no other reason than it is the Mozilla version
on this machine) outputs the warning:-

"Warning: reference to undefined property window.clipboar dData"
It seems this no longer happens in newer versions. (1.6 is three
years old.)
It is good to hear that someone has finally come to their senses and
removed that nonsense. It remains depressing to think of all of the
examples of needlessly convoluted code uses in place of simpler
alternatives in order to avoid those "warnings" by people who would (or
could) not see that the warning made no sense in ECMAScript terms and
instead preferred to think of the authors of Mozilla as all-knowing
authorities on the writing of browser scripts (an extremely questionable
position even if the "warning" had not now been rescinded).
>Given the code:-

function testFunc(param) {
var param; // This is a "bad" application of the otherwise "good"
// declaration of local variables.
}

Mozilla outputs the warning:-

"Warning: variable param hides argument"

- and that is an absolute lie. ECMA 262 is very clear about how
such code should be handled (Section 10.1.3) and not only does
the - var param; - not "hide" the - param - formal parameter it
is actually specified as doing nothing

Right, that's how it works, and the warning is indeed a bit
misleading.
"A bit misleading"?
Thanks for mentioning it.
I suppose I should have a go at tracking down the other couple of
spectacularly bad "warnings" that have been brought to my attention over
the years.

<snip>
>>You could save me and yourself some time by only posting
this. You answered my question.
<snip>

I was interested in whether you would present any compelling
arguments for changing the FAQ. You have not.
There are no compelling arguments for changing it if you don't
think that putting code in string literals for no reason is
wrong.
Doesn't that swing on "for no reason". We know that there was a reason
for putting the code in strings in the first place (that it was
originally done at a time when the alternative was non-viable for
cross-browser work). That reason is probably no longer relevant.
I hoped somebody would tell me the reason for *not*
changing it.
I don't think that there is still a reason for not changing it, on the
other hand, I have not seen a positive reason for changing it either.
Apparently
people here just like the Function constructor more.
<snip>

I don't think that is true. I don't think anyone should give in to an
assertion that the use of the Function constructor is always bad without
that being backed up with a pretty solid justification for that position
(something beyond the observation that its use can be objectively bad (on
many fronts) being extended into an absurd blanket injunction against its
use).

An ideal situation exists when individuals find themselves in a position
to assess the merits of any particular technique on an informed basis and
with a consideration of the context in which it may be applied. In such
circumstances there is no need for black and white injunctions, or
declarations of good and bad, as, where valid, those conclusions will
inevitably follow from understanding.

Richard.

Jan 28 '07 #14
On Jan 29, 12:15 am, "Richard Cornford" <Rich...@litote s.demon.co.uk>
wrote:
I hoped somebody would tell me the reason for *not*
changing it.
I don't think that there is still a reason for not changing it, on the
other hand, I have not seen a positive reason for changing it either.
You are right. Perhaps both variants could be demonstrated.

Nickolay

Jan 29 '07 #15

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

Similar topics

5
3455
by: Paradox | last post by:
I would like to call a function whose name I supply at runtime. Something like this but it didn't work functionName = 'run' instance = ClassDef() args = #want the dynamic equivalant of #instance.run(*args)
3
14958
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
9
2162
by: Andy Lomax | last post by:
gcc 3.3 allowed a typedef for a function pointer to contain default argument specifications; for example: typedef int (*foo) (int, int, int=0, bar=0); However, it turns out that this is illegal and gcc 3.4 prohibits it. Problem: I've got a lot of code that uses this 'feature', and I can't move the default specifications into the function declaration or definition, because they're not visible. This means that I can no longer call...
12
38521
by: Huskier | last post by:
Hi all: I want to pass a class-member function to pthread_create, and my code is in the following, but I don't know how to pass myobj.thread_function to pthread_create function. #include <pthread.h> class test { public:
5
2172
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
11
21935
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a C# equivalent. Any ideas? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. varnk@diebold.com
10
2304
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px values. Works in both ie and firefox. Parameters styleType = top | left | width | height toNumber = the new value of the style then you pass in as many ids as you would like.
9
2746
by: Khookie | last post by:
Hi all, I was wondering what was considered macro abuse. Specifically I implemented a key-value array (or dictionary), as per below. Notice the use of macros - would that be considered alright? #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
9
1597
by: Gilles Ganault | last post by:
Hello As a newbie, it's pretty likely that there's a smarter way to do this, so I'd like to check with the experts: I need to try calling a function 5 times. If successful, move on; If not, print an error message, and exit the program: ===== success = None
0
9706
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
10571
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...
1
10317
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
9143
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
7615
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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
3815
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.