getFunctionName v1.0

V

VK

And this script supposes to work everywhere. It allows to get the name
of any function from within the function itself.

<html>
<head>
<title>Function name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<script type="text/javascript">

function test() {
var myName = getFunctionName(arguments.callee);
alert(myName);
}

function getFunctionName(f) {
var tmp = f.toString();
var re = /(\s*function\s+)(\w+)(\s*\()/m;
re.exec(tmp);
return RegExp.$2;
}

</script>
</head>

<body bgcolor="#FFFFFF" onload="test()">

</body>
</html>
 
B

Baconbutty

In the past I have found that approach does not work for anonymous
functions (by definition!).

var anon=function()
{
var myName = getFunctionName(arguments.call­ee);
alert(myName);
};

anon();
 
V

VK

Baconbutty said:
In the past I have found that approach does not work for anonymous
functions (by definition!).

var anon=function()
{
var myName = getFunctionName(arguments.call­ee);
alert(myName);
};

anon();

But how does it suppose to work for anonymous functions? Anonymous
functions do not have names, this is why they are anonymous!
getFunctionName will return in such case the string "anonymous", and
it's holly truth.
I could change it into some getNameOrNameIt, so it would clone
anonymous functions on the spot and give them names, but it would be
the whole new story. And why?
 
B

Baconbutty

But how does it suppose to work for anonymous functions? Anonymous
functions do not have names, this is why they are anonymous!

That is the point that I was making.

And it can never work because more than one variable could hold a
reference to the anonymous function. Some people do get confused with
that.

See my notes on serialisation to your other post.
 
V

VK

Baconbutty said:
That is the point that I was making.

And it can never work because more than one variable could hold a
reference to the anonymous function.

Never say never possible!
It's the first rule of every web-development victim :)

But what situation are you conserned about? I'm just a bit cloudy here.

What anonymous functions can stop you from serialization? Intrinsic
event handlers or custom objects' methods? I don't see any other
*valid* reasons to have anon functions. Or yeh, and runtime assemply
using Function() but it's ugly and I did not see it in a wide use.

Could you give some code like "this and that, and here we have to stop
because..." ?
 
V

VK

Dr said:
It does not work for me as posted, using IE4.

Edit me if I'm mistaken but IE4 (Oct. 1997) is out of the scope of
support in the year 2005, as well as NN4.
BTW: IE3.1 was released just one year earlier (Oct. 1996), so why
wouldn't include it also?

Actually IE5 (Mar. 1999) should be forgotten by all means too (6 years
is the age of dinosaurs for browsers). It remains a bizarre exception
because in 6 years and till now Macintosh did not manage to produce ane
decent browser for their OS. So we semi stoke with this ancient product
when a maximum compatibility is needed.

IE5.5 (Jul. 2000) is really a border line. After all Windows users will
finally migrate on XP SP2 (no latter than the end of this year), we can
forget this twilight product with the greatest relief.
 
L

Lasse Reichstein Nielsen

D
VK said:
After all Windows users will finally migrate on XP SP2 (no latter
than the end of this year), we can forget this twilight product with
the greatest relief.

Unless Microsoft starts giving XP away for free, I think it's highly
unlikely that home users will be more likely to upgrade then than now.

/L
 
M

Matt Kruse

VK said:
Edit me if I'm mistaken but IE4 (Oct. 1997) is out of the scope of
support in the year 2005, as well as NN4.

Why? Because you say so?

Different people have different requirements. I know a number of situations
where NN4 support is still required, and at least several where IE4 support
is required.

It all depends.
After all Windows users
will finally migrate on XP SP2 (no latter than the end of this year),
we can forget this twilight product with the greatest relief.

MANY corporate users are still on Windows 2000, with no plans to migrate to
XP.
 
M

Michael Winter

Edit me if I'm mistaken but IE4 (Oct. 1997) is out of the scope of
support in the year 2005, as well as NN4.

There are different degrees of support. Expecting browsers of that
generation to be capable of the same things as current browsers is
unrealistic, and on that basis it's fair to exclude them. However, that
doesn't mean code should fail uncontrollably, causing errors or, in the
worst case, crashing the browser.
BTW: IE3.1 was released just one year earlier (Oct. 1996), so why
wouldn't include it also?

IE3 is /significantly/ less capable than IE4 and, though IE4 is
relatively rare, IE3 is non-existent.

[snipped tangent]

As for IE4 compatibility, remove the multi-line flag. You don't need it
anyway.

Mike
 
R

RobG

VK said:
Edit me if I'm mistaken but IE4 (Oct. 1997) is out of the scope of
support in the year 2005, as well as NN4.
BTW: IE3.1 was released just one year earlier (Oct. 1996), so why
wouldn't include it also?

Actually IE5 (Mar. 1999) should be forgotten by all means too (6 years
is the age of dinosaurs for browsers).

As it's been 4 years since the release of IE6, I guess you'll be
dropping support for it pretty soon too... ;-)

It's interesting to note the release dates for IE versions:

Version 1: 1995
Version 2: 1995
Version 3: 1996
Version 4: 1997
Version 5: 1998
Version 6: 2001
Version 7: 2006?

Based on the above, I presume we will see IE 8 sometime in 2015. Check
out the link, it's good for a giggle:

is the age of dinosaurs for browsers). It remains a bizarre exception
because in 6 years and till now Macintosh did not manage to produce ane
decent browser for their OS. So we semi stoke with this ancient product
when a maximum compatibility is needed.

I guess you shouldn't ever depend on MS to support any product for any
platform other than their own (are there any left besides Office: Mac?).
There are a number of browsers available for Mac OS that are more
standards compliant than IE 5.1/2 - and they're free.

Unfortunately, Apple have chosen to only release feature upgrades to
Safari with the purchase of a new OS (just like Microsoft), but given
that Firefox/Mozilla/Netscape provides a more than adequate (free)
alternative, what's the issue?

For corporate environments where support for particular features is
required, use a suitable browser (there are plenty of them). For the
WWW, Safari is just another bundle of issues to deal with.
IE5.5 (Jul. 2000) is really a border line. After all Windows users will
finally migrate on XP SP2 (no latter than the end of this year), we can
forget this twilight product with the greatest relief.

It is more likely that IE5 will vanish because users migrate to
alternative browsers than to some upgrade of Windows. Since IE is
almost essential for Windows Update (and that is a monthly affair for
those who would stay 'secure') I can't see it totally disappearing.
 
B

Baconbutty

Never say never possible!

True enough!
But what situation are you conserned about? I'm just a bit cloudy here.
I don't see any other *valid* reasons to have anon functions.

As well as for web pages, I also use Javascript to write mini
browser-applications for use at my work (not intended to be
cross-browser). I am a hobby programmer with limited time to learn
powerful languages, DHTML provides me with a way of rapidly
developing simple applications.

As part of this, I have been creating my own Javascript library to
enable code reuse.

I structure the library using "pseudo" namespaces (probably bad
practice)

E.g.

var SYSTEM=function(){}; // BASE

var SYSTEM.TOOLS=function(){}; // A sub-namespace

SYSTEM.TOOLS.AGenericDataObject=function()
{
this.someData="";
};

Then I can call on the data object in any give application that imports
SYSTEM.

var oInstance=new SYSTEM.TOOLS.AGenericDataObject();

Clearly to be able to serialise that data object I need to get its
constructor - AGenericDataObject, but as it is an anonymous function, I
had already discovered that the method you pointed out did not work.
Accordingly I needed to find some other way of informing code as to the
constructor.

My quick answer was to include the following (only in objects I wanted
to serialise mind-you):-

SYSTEM.TOOLS.AGenericDataObject=function()
{
this.CONSTRUCTOR="SYSTEM.TOOLS.AGenericDataObject";
};

There may be better more elegant solutions.
 
C

Christopher J. Hahn

VK wrote:
[snip]
IE5.5 (Jul. 2000) is really a border line. After all Windows users will
finally migrate on XP SP2 (no latter than the end of this year), we can
forget this twilight product with the greatest relief.

I have absolutely no intention of ever upgrading to XP, regardless of
what Service Pack it's on.

In my office, also, about 40 - 50% of our machines still run Windows
2000. I also have a second 2k box at home, and know many other people
who still run it.

Never underestimate inertia.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Tue, 26 Jul 2005 10:59:05, seen in VK
Edit me if I'm mistaken but IE4 (Oct. 1997) is out of the scope of
support in the year 2005, as well as NN4.

You stated "And this script supposes to work everywhere."

Being a regular reader of this group, you should have known that IE4 is
still in use.

Everywhere means everywhere; you were wrong; your advice cannot be
relied upon.
 
V

VK

About anonymous function:
Interestingly enough, both IE and FF (but not Opera) have two different
string representations for anonymous functions:

1) "stay-alone" function via Function() or an intrinsic handler:
"function anonymous().."

2) a method of an object:
"function()..."

Opera always has the 2nd variant.

So if my getFunctionName returns empty string and it's not Opera, you
can be sure that you've got a reference to an object method.


P.S. The code below tested with The NSCA Mosaic and rendered properly.

P.P.S. The only site I've found so far that would be viewable and
operable with Mosaic is www.w3.org. No wonder their site is the ugly as
a sin. :)



<html>
<head>
<title>Function name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!--

function ObjectConstructor() {
this.method = function(){alert('ocMessage');}
}

var oInstance = new ObjectConstructor();

var fInstance = new Function("alert('fInstanceMessage');");

function test() {
//var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
alert(getFunctionName(arguments.callee)); // 'test'
alert(getFunctionName(fInstance)); // 'anonymous' || '' (Opera)
alert(getFunctionName(ObjectConstructor)); // 'ObjectConstructor'
alert(getFunctionName(oInstance.method)); // true
}

function getFunctionName(f) {
fName = '';
var fName = '';
var fCode = f.toString();
var re = /(function\s*)(\w+)(\s*\()/;
if (re.test(fCode)) {
re.exec(fCode);
fName = RegExp.$2;
}
return fName;
}

window.onload = test;
//-->
</script>
</head>

<body>
<noscript>
<h4>JavaScript is not supported or turned off.
This page may not operate as being designed.</h4>
</noscript>
</body>
</html>
 
G

Grant Wagner

VK said:
And this script supposes to work everywhere. It allows to get the name
of any function from within the function itself.

<html>
<head>
<title>Function name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<script type="text/javascript">

function test() {
var myName = getFunctionName(arguments.callee);
alert(myName);
}

function getFunctionName(f) {
var tmp = f.toString();
var re = /(\s*function\s+)(\w+)(\s*\()/m;
re.exec(tmp);
return RegExp.$2;
}

</script>
</head>

<body bgcolor="#FFFFFF" onload="test()">

</body>
</html>

Bzzzz! Nice try. Thanks for playing. We have some lovely parting gifts
for you.

Consider:

<script type="text/javascript">
function test() {
var myName = getFunctionName(arguments.callee);
alert(myName);
}
function getFunctionName(f) {
var tmp = f.toString();
var re = /(\s*function\s+)(\w+)(\s*\()/m;
re.exec(tmp);
return RegExp.$2;
}
var a = test;
test = null;
a();
</script>
 
M

Matt Kruse

Grant said:
Consider:
<script type="text/javascript">
function test() {
var myName = getFunctionName(arguments.callee);
alert(myName);
}
function getFunctionName(f) {
var tmp = f.toString();
var re = /(\s*function\s+)(\w+)(\s*\()/m;
re.exec(tmp);
return RegExp.$2;
}
var a = test;
test = null;
a();
</script>

Thus demonstrating the futility of even trying to get a function's name.

Variable and function names are for humans. Internally, they're just
references to memory addresses that hold data. Once compiled, the computer
doesn't know or care what name you gave it in the code.

Hacking a way to get to the name of the function that is running will
definitely work in some cases, and definitely always fail in other cases.

If you're willing to only stick to coding styles and browsers where it will
be successful, then it might be useful to you. But any such function should
come with heavy disclaimers making it clear that it doesn't really do what
it claims to do. It just fakes it in some cases.
 
V

VK

Being a regular reader of this group, you should have known that IE4 is
still in use.

As well as the "Pan 0.13.0 : The Whole Remains Beautiful" browser.
Who should pay to satisfy few individuals across the world who prefer
to use anscient or exclusive products? The absolute max spend on them
would be to put a global message-generating wrapper on your script plus
a dublicate in <noscript> section. To put some ground-based countdown
into my words: Microsoft itself currently supports only Internet
Explorer 5.1 and higher (see their support section).

Being a regular reader of this group you may notice and participate
this thread:

<http://groups-beta.google.com/group...+insubject:2005&rnum=1&hl=en#0165a598d06dc3da>

I need to repeat that IE5.0 for Mac remains a *forced exception* for
the list above *if you want to go really global*. Mac itself did not
manage to do anything good as browser, and that IE5.0 still rather
popular on Apple platforms. It has beem made nearly as personal present
from Bill Gates to Steve Jobs (why - goes too far offtopic) using
provided open source codes of Mac OS. This is why it has exellent
rendering abilities. Unfortunately Firefox for Mac has some serious
productivity ussies (lesser on Linux). And who wants to see his/her
$2000 beauty acting like PC AT ?

And I looked through the Safary. My guess was right: it's the same very
good "NN4 for Mac" engine patched here and there with *very selected*
pieces of modern technologies. Someone just took the license-freed code
from AOL and did hash-hash with it for OS X release. If you're starving
of hostalgia, you even can use again <layer..> <nolayers>... blocks.
(Who understands and remembers - put the tears off your eyes :)

Everywhere means everywhere; you were wrong; your advice cannot be
relied upon.

Oh com'on... Read the linked topic.
 
V

VK

Thus demonstrating the futility of even trying to get a function's name.

How much you bet?

IE5.5 and higher / FF 1.0.3 and higher / Opera 8.0 and higher
(no IE4, NN2 etc. - sorry)


Overall real comment should be done on the code like this:

var obj1 = new Object();
var obj2 = obj1;

<COMMENT>
Never ever make multiple references on the same object, however it
would seem necessary by the program logic! (check your logic instead).
Multiple references is the most effective way to bring your memory into
the trashhold state.
</COMMENT>


So how much you bet? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top