473,407 Members | 2,306 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,407 software developers and data experts.

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>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>

Jul 25 '05 #1
34 2082
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();

Jul 26 '05 #2
VK


Baconbutty wrote:
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?

Jul 26 '05 #3
>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.
And why?


See my notes on serialisation to your other post.

Jul 26 '05 #4
VK


Baconbutty wrote:
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.


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..." ?

Jul 26 '05 #5
JRS: In article <11**********************@g14g2000cwa.googlegroups .com>
, dated Mon, 25 Jul 2005 13:43:23, seen in news:comp.lang.javascript, VK
<sc**********@yahoo.com> posted :
And this script supposes to work everywhere.
It does not work for me as posted, using IE4.

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


One should not set a background colour with an unknown foreground
colour, not vice versa.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 26 '05 #6
VK
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). 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.

Jul 26 '05 #7
D
"VK" <sc**********@yahoo.com> writes:
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
--
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 26 '05 #8
VK wrote:
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 26 '05 #9
On 26/07/2005 18:59, 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.


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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 26 '05 #10
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.com/windows/WinHistoryIE.mspx>
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=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.

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>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>

Jul 27 '05 #14
"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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>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>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 27 '05 #15
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Tue, 26 Jul 2005 10:59:05, seen in news:comp.lang.javascript, VK
<sc**********@yahoo.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.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.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.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.

--
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.javascript/browse_frm/thread/cf0511d0d133352/0165a598d06dc3da?lnk=st&q=group:comp.lang.javascri pt+insubject:Summer+insubject:2005&rnum=1&hl=en#01 65a598d06dc3da>

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
VK wrote:
Being a regular reader of this group you may notice and participate
this thread:

<http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/cf0511d0d133352/0165a598d06dc3da?lnk=st&q=group:comp.lang.javascri pt+insubject:Summer+insubject:2005&rnum=1&hl=en#01 65a598d06dc3da>

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 [sic]. 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 :-)


Uh... Safari has nothing to do with NN4. It is based on KHTML and KJS
from the KDE Konqueror browser. It seems as fast and standards
compliant as any of the other popular browsers.

Good luck to you if you want to alienate all the Mac users out there
(yeah, yeah, only 3% market share). Safari has a "Report a Bug" button
in its toolbar. If enough people click it for your web sites, Steve
Jobs will send his goons to have a private "chat" with you. :)

Jul 27 '05 #21
VK
> Good luck to you if you want to alienate all the Mac users out there
(yeah, yeah, only 3% market share).


As I said: "I need to repeat that IE5.0 for Mac remains a *forced
exception* for
the list above *if you want to go really global*."

And Safary is simply out of reach for debugging for Windows platform. I
had to look around for a while to find a friend of friend of mine just
to see Safary in action. But I cannot take it home neither I'm buying a
"debugging Macintosh" :-(

Just FYI: Steven King uses Macintosh only. It doesn't say what browser,
but it is said that he never shanges anything as it came out of the
box. So I guess he's using Safary now :-)

Jul 27 '05 #22
VK wrote:
And Safary [sic] is simply out of reach for debugging for Windows platform. I
had to look around for a while to find a friend of friend of mine just
to see Safary [sic] in action. But I cannot take it home neither I'm buying a
"debugging Macintosh" :-(


But the Mac Mini is so cute. Its target market is cross platform
development. Apple sez:

Perfect for Programmers

Set a space-saving Mac mini atop your PC workstation
and add a KVM switch to share keyboard, monitor and
mouse. Mac OS X includes free developer tools for Mac,
UNIX and Java. Relegate that PC to the testbed.

Jul 27 '05 #23
VK wrote:
Thus demonstrating the futility of even trying to get a
function's name.
How much you bet?


"Demonstrating the futility" seems a good summation of this thread.
IE5.5 and higher / FF 1.0.3 and higher / Opera 8.0 and
higher (no IE4, NN2 etc. - sorry)
Not even close to cross-browser then.
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>
Another near incoherent expression of ill-informed and inaccurate bad
advice. Making this nonsense up off the top of your head and writing it
down doesn't make it true. More reasonable advice would be that if you
find yourself concerned by a need to discover the defined name of a
function you are doing something wrong (and would be better checking
your logic at that point).
So how much you bet? :-)


Anything you like. You would be better of learning javascript before
pontificating about it.

Richard.
Jul 27 '05 #24
"VK" <sc**********@yahoo.com> writes:
<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>


Where did you pick up that idea? Only having one reference to each
object would make most object oriented programming impossible. Even
passing an object reference as a parameter to a method would be
impossible.

OO language are build to allow multiple references to objects, so that
alone is not a problem for memory (whatever a "trashhold state" is).

/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 28 '05 #25
VK


Lasse Reichstein Nielsen wrote:
"VK" <sc**********@yahoo.com> writes:
<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>


Where did you pick up that idea? Only having one reference to each
object would make most object oriented programming impossible. Even
passing an object reference as a parameter to a method would be
impossible.

OO language are build to allow multiple references to objects, so that
alone is not a problem for memory (whatever a "trashhold state" is).

Please do not substitute the subject. Passing object reference to
functions and constructors is the base of OP, it would be stupid to
talk against it. But in such case you have a call chain that:
1) allows engine/you to dismiss objects automatically or manually
2) allows you to backtrace the original literal name (side effect).

By explicetly doing somethin like:

var ref1 = new someObject();
ref2 = ref1;

or even:

var ref1 = new someObject();
ref2 = ref1;
ref1 = newValue;

you're getting 90% guarantee that this instance will be never
dismissed: neither by you (because the literal is not available) nor by
engine (because you twisted its brains off). Good if it's some Object()
based crap: it may go away eventually after unload. Bad is it's an
automation object: you'll end up with running copies of it until the OS
restart.

Jul 28 '05 #26
I am not sure actually. I probably have not reflected on it.

I'll do a simple search and replace, and do as you suggest.

Jul 28 '05 #27
VK wrote:
Lasse Reichstein Nielsen wrote:
"VK" <sc**********@yahoo.com> writes:
<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>
Where did you pick up that idea? Only having one reference
to each object would make most object oriented programming
impossible. Even passing an object reference as a parameter
to a method would be impossible.

OO language are build to allow multiple references to
objects, so that alone is not a problem for memory
(whatever a "trashhold state" is).


Please do not substitute the subject.


Subjects are at best the starting point for a thread. Threads then go
off wherever they will.

<snip> By explicetly doing somethin like:

var ref1 = new someObject();
ref2 = ref1;

or even:

var ref1 = new someObject();
ref2 = ref1;
ref1 = newValue;

you're getting 90% guarantee that this instance will be never
dismissed: neither by you (because the literal is not
available) nor by engine (because you twisted its brains off).
Rubbish. Assuming that 'dismissed' is a reference to garbage collection
then there is no issue here. Javascript garbage collection has no
problems handling multiple references to the same object.
Good if it's some Object() based crap: it may go
away eventually after unload. Bad is it's an automation
object: you'll end up with running copies of it
until the OS restart.


Learning javascript and browser scripting would be more productive than
making up voodoo stories about it and then putting them across as if you
know something about the subject.

Richard.
Jul 28 '05 #28
VK wrote:
[snip]
you're getting 90% guarantee that this instance will be never
dismissed: neither by you (because the literal is not available) nor by
engine (because you twisted its brains off). Good if it's some Object()
based crap: it may go away eventually after unload. Bad is it's an
automation object: you'll end up with running copies of it until the OS
restart.


You're a PERL guy, right? Garbage collection in JS works just like
PERL's.

Whenever a variable's internal reference count reaches 0, it is
considered disposable and is disposed of.

The following lines do the exact same thing in JS as they do in PERL
(copy/paste them if you want):
$x = { };
$y = $x;
$x = { };
$y = { };

They differ only in that in JS { } is an object literal, and in PERL {
} is a hash literal, but that's immaterial to this discussion.

The thing created on line 1 is not garbage collected until line 4, and
it will ALWAYS be garbage collected at line 4. You can depend upon it.
There is no mystery to when an object is disposed of.

It is disposed of when the number of references to it reaches zero.
Plain and simple. No 'eventually' about it.

Period.

Jul 28 '05 #29
VK
> You're a PERL guy, right? Garbage collection in JS works just like
PERL's.
I had to work a lot with Perl during The Bubble, but it was a "forced
marriage" (good contract = CGI = Perl).
Personally I think it was a language the least suited for Web. Just one
of the Bubble's twists: it was available, it was semi-working, it was
free and opps... it's a world wide standard.

The memory issues never bothered me there as I knew Perl only from its
CGI-script side. And on each shared server there was a "serial object
killer" launched by admin. After the assigned deadline it would simply
kill your context no matter was it in use or trach leftovers. So the
task was to do it as quick as possible and get out :-)

Whenever a variable's internal reference count reaches 0, it is
considered disposable and is disposed of.

The following lines do the exact same thing in JS as they do in PERL
(copy/paste them if you want):
$x = { };
$y = $x;
$x = { };
$y = { };

They differ only in that in JS { } is an object literal, and in PERL {
} is a hash literal, but that's immaterial to this discussion.

The thing created on line 1 is not garbage collected until line 4, and
it will ALWAYS be garbage collected at line 4. You can depend upon it.
There is no mystery to when an object is disposed of.

It is disposed of when the number of references to it reaches zero.
Plain and simple. No 'eventually' about it.

Period.


That's a great piece of info if you really sure about it!

Still I cannot understand why would anyone need two co-existing
references to the same object in the same namespace. For sport? To
check the garbage collector? JavaScript fully supports object
construction, extention and inheritance. If you really want to stay
within OP that would be plenty enough.

Jul 28 '05 #30
VK
> You're a PERL guy, right? Garbage collection in JS works just like
PERL's.
I had to work a lot with Perl during The Bubble, but it was a "forced
marriage" (good contract = CGI = Perl).
Personally I think it was a language the least suited for Web. Just one
of the Bubble's twists: it was available, it was semi-working, it was
free and opps... it's a world wide standard.

The memory issues never bothered me there as I knew Perl only from its
CGI-script side. And on each shared server there was a "serial object
killer" launched by admin. After the assigned deadline it would simply
kill your context no matter was it in use or trach leftovers. So the
task was to do it as quick as possible and get out :-)

Whenever a variable's internal reference count reaches 0, it is
considered disposable and is disposed of.

The following lines do the exact same thing in JS as they do in PERL
(copy/paste them if you want):
$x = { };
$y = $x;
$x = { };
$y = { };

They differ only in that in JS { } is an object literal, and in PERL {
} is a hash literal, but that's immaterial to this discussion.

The thing created on line 1 is not garbage collected until line 4, and
it will ALWAYS be garbage collected at line 4. You can depend upon it.
There is no mystery to when an object is disposed of.

It is disposed of when the number of references to it reaches zero.
Plain and simple. No 'eventually' about it.

Period.


That's a great piece of info if you really sure about it!

Still I cannot understand why would anyone need two co-existing
references to the same object in the same namespace. For sport? To
check the garbage collector? JavaScript fully supports object
construction, extention and inheritance. If you really want to stay
within OP that would be plenty enough.

Jul 28 '05 #31
JRS: In article <11*********************@g43g2000cwa.googlegroups. com>,
dated Wed, 27 Jul 2005 13:05:05, seen in news:comp.lang.javascript, VK
<sc**********@yahoo.com> posted :
Lines: 39

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


As well as


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


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


The fact remains that you made a false statement, either by intention or
by carelessness. Your code did not work everywhere, not even in all
javascript browsers in current use.

--
© John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.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 28 '05 #32
"VK" <sc**********@yahoo.com> writes:
engine (because you twisted its brains off). Good if it's some Object()


You know, it was only through a great deal of restraint that I prevented
myself from spraying my keyboard with soda after the "trashhold" thing,
then you have to go and write about twisting a GC's brains off! Now
look at the mess I have!

--
Jason Dufair - ja**@dufair.org
http://www.dufair.org/
Crazy just thinkin' knowing that the world is round
And here I'm dancing on the ground
Never right side up or upside down
Is this real or am I dreaming?
-- Dave Matthews Band, "Crush"
Jul 28 '05 #33
VK
Jason Dufair wrote:
You know, it was only through a great deal of restraint that I prevented
myself from spraying my keyboard with soda after the "trashhold" thing,
then you have to go and write about twisting a GC's brains off! Now
look at the mess I have!


<FLUD>
It's just nothing. But if you see a thread here about *class* or *hash*
you better read it without a coffee in your hand or some unexpected and
painful physical damage is nearly guaranteed.
</FLUD>

Jul 28 '05 #34
VK wrote:
You're a PERL guy, right? Garbage collection in JS works just like
PERL's.
I had to work a lot with Perl during The Bubble, but it was a "forced
marriage" (good contract = CGI = Perl).
Personally I think it was a language the least suited for Web. Just one
of the Bubble's twists: it was available, it was semi-working, it was
free and opps... it's a world wide standard.


No oops about it... if you need fast code, do C. If you need code fast,
do PERL.
The memory issues never bothered me there as I knew Perl only from its
CGI-script side.
In which memory issues can and often are far more prevalent.
And on each shared server there was a "serial object
killer" launched by admin. After the assigned deadline it would simply
kill your context no matter was it in use or trach leftovers. So the
task was to do it as quick as possible and get out :-)
Isn't it always?
Whenever a variable's internal reference count reaches 0, it is
considered disposable and is disposed of.
$x = { };
$y = $x;
$x = { };
$y = { };

The thing created on line 1 is not garbage collected until line 4, and
it will ALWAYS be garbage collected at line 4. You can depend upon it.
There is no mystery to when an object is disposed of.

It is disposed of when the number of references to it reaches zero.
Plain and simple. No 'eventually' about it.

Period.


That's a great piece of info if you really sure about it!


Technically it becomes "available for disposal". In practice the engine
might dispose of it in a few nano- or micro- seconds. By spec I believe
it is *allowed* to wait until it's damned good and ready, but the point
is that after the object's internal reference count reaches 0 it
becomes unreferrable and it *will* certainly be garbage-collected.

There are certainly ways to defeat this if you want to, but garbage
collection is fairly well-defined and dependable, and you can predict
what will be disposed of if you understand what you're coding.

*IF* you understand what you're doing.

Something we should all strive for, yes?
Still I cannot understand why would anyone need two co-existing
references to the same object in the same namespace. For sport? To
check the garbage collector? JavaScript fully supports object
construction, extention and inheritance. If you really want to stay
within OP that would be plenty enough.


For simplicity, efficiency, maintainability, readbility, and plain
elegance of code.

Consider this:

var c = 0,
things = [
new Thing(1),
new Thing(2),
// ...
new Thing(20)
];
/* the next statement creates a second reference to an existing object
* that already has a reference stored in the things[] array.
*/
while( thing = things[ ++c ] ) {
// figure out some x and y coords
thing.position( x, y );
thing.show();
}
Almost any time you need to perform multiple operations on an arbitrary
object independent of its name, it doesn't make sense but to store a
second reference to it.

Why wouldn't you?

Jul 29 '05 #35

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

Similar topics

3
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...
6
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...
4
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
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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...

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.