473,790 Members | 3,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getFunctionName v1.0

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>Functi on name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<script type="text/javascript">

function test() {
var myName = getFunctionName (arguments.call ee);
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="#FFFFF F" onload="test()" >

</body>
</html>

Jul 25 '05
34 2134
VK wrote:
Dr John Stockton wrote:
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).


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:

<URL:http://www.microsoft.c om/windows/WinHistoryIE.ms px>
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.

--
Rob
Jul 27 '05 #11
>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=fu nction(){}; // A sub-namespace

SYSTEM.TOOLS.AG enericDataObjec t=function()
{
this.someData=" ";
};

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

var oInstance=new SYSTEM.TOOLS.AG enericDataObjec t();

Clearly to be able to serialise that data object I need to get its
constructor - AGenericDataObj ect, 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.AG enericDataObjec t=function()
{
this.CONSTRUCTO R="SYSTEM.TOOLS .AGenericDataOb ject";
};

There may be better more elegant solutions.

Jul 27 '05 #12
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.

Jul 27 '05 #13
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>Functi on name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!--

function ObjectConstruct or() {
this.method = function(){aler t('ocMessage'); }
}

var oInstance = new ObjectConstruct or();

var fInstance = new Function("alert ('fInstanceMess age');");

function test() {
//var isOpera = (navigator.user Agent.indexOf(' Opera') != -1);
alert(getFuncti onName(argument s.callee)); // 'test'
alert(getFuncti onName(fInstanc e)); // 'anonymous' || '' (Opera)
alert(getFuncti onName(ObjectCo nstructor)); // 'ObjectConstruc tor'
alert(getFuncti onName(oInstanc e.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>JavaScrip t is not supported or turned off.
This page may not operate as being designed.</h4>
</noscript>
</body>
</html>

Jul 27 '05 #14
"VK" <sc**********@y ahoo.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
And this script supposes to work everywhere. It allows to get the name
of any function from within the function itself.

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

<script type="text/javascript">

function test() {
var myName = getFunctionName (arguments.call ee);
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="#FFFFF F" 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.call ee);
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>

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 27 '05 #15
JRS: In article <11************ **********@f14g 2000cwb.googleg roups.com>
, dated Tue, 26 Jul 2005 10:59:05, seen in news:comp.lang. javascript, VK
<sc**********@y ahoo.com> posted :
Dr John Stockton wrote:
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.


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.

--
© John Stockton, Surrey, UK. ??*@merlyn.demo n.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Jul 27 '05 #16
Baconbutty wrote:
var SYSTEM=function (){}; // BASE


Why a function instead of

var SYSTEM = {}; // BASE

?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 27 '05 #17
Grant Wagner wrote:
Consider:
<script type="text/javascript">
function test() {
var myName = getFunctionName (arguments.call ee);
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 27 '05 #18
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/comp.lang.javas cript/browse_frm/thread/cf0511d0d133352/0165a598d06dc3d a?lnk=st&q=grou p:comp.lang.jav ascript+insubje ct:Summer+insub ject:2005&rnum= 1&hl=en#0165a59 8d06dc3da>

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.

Jul 27 '05 #19
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? :-)

Jul 27 '05 #20

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

Similar topics

3
25096
by: Daniel Hansen | last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now... Is there a PHP function or global variable that will return name of the calling function? I want to do this for error reporting purposes without having to hardcode the function name into my scripts. Say the function is named getFunctionName(). I want to do something like... function foo() {
6
6079
by: Richard Berg | last post by:
Hello, I need implementation advice: I am writing a program that should call some API functions in a specific order. The APIs are for another application, not Windows APIs. The order and type of the calls is determined at runtime. There are a couple of hundred of those APIs that can be called, and they all take different parameters. What I need is somehow queue the calls with their parameters and then dequeue and execute them one by...
4
3820
by: Hrvoje Voda | last post by:
How to get a list of names from database into string array? variable name should be string array... This is the code I use... public string GetFunctionName(Guid UserID) {
0
9512
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
10419
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
10201
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
10147
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
9987
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
9023
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
7531
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
5424
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
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.