Scope of constructed objects

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

Just to make sure I'm on the right track...

function getADate() {
return new Date();
}

function foo() {
var bar=getADate();
alert( bar );
}

This doesn't work as one might expect because the date constructed by
getADate() is destroyed once the function returns, meaning that bar
is a reference to a destroyed object when alert() is called. Is that
correct?
 
L

Lee

Christopher Benson-Manica said:
Just to make sure I'm on the right track...

function getADate() {
return new Date();
}

function foo() {
var bar=getADate();
alert( bar );
}

This doesn't work as one might expect because the date constructed by
getADate() is destroyed once the function returns, meaning that bar
is a reference to a destroyed object when alert() is called. Is that
correct?

No. It works as I expect, alerting the current date and time.
Local variables are destroyed once a function returns, but there
are no local variables in getADate(). The Date object survives
because there is still a reference to it. Once foo() returns,
the last reference to that Date object is destroyed and it will
be subject to garbage collection.
 
C

Christopher Benson-Manica

Lee said:
No. It works as I expect, alerting the current date and time.
Local variables are destroyed once a function returns, but there
are no local variables in getADate(). The Date object survives
because there is still a reference to it. Once foo() returns,
the last reference to that Date object is destroyed and it will
be subject to garbage collection.

So why, then, does a reference to a variable in another window become
invalid when that window is destroyed?
 
C

Christopher Benson-Manica

Duncan Booth said:
It doesn't. References never become invalid although the objects they
reference may mutate and stop offering functionality they once had.

Why would the object mutate? I'm getting a little bit confused here.
If you make a reference to an object in another window, the object will
exist beyond the lifetime of the window so long as your reference is still
reachable from your code.

Define "still reachable" - I tested this exact situation, and a Date
object from another window seemed to "mutate" (it was still an object,
but it didn't appear to be a Date object any more).
 
C

Christopher Benson-Manica

Duncan Booth said:
If you make a reference to an object in another window, the object will
exist beyond the lifetime of the window so long as your reference is still
reachable from your code.

Here's the test I performed - the Date object is intact before the
window closes, but afterward it's mutated into something else...

<html>
<head>
<script type="text/javascript">
var date, pwin;
function clickIt() {
alert( date );
}
function setDate( adate ) {
date=adate;
alert( date );
pwin.close();
}
function pop() {
pwin=window.open( '' );
pwin.document.writeln( '<html>'+
' <head>'+
' <script type="text/javascript">'+
' var date=new Date();'+
' window.opener.setDate( date );'+
'<\/script><\/head><\/html>' );
}
</script>
</head>
<body>
<input type="button" value="click" onclick="clickIt()"><br>
<input type="button" value="pop" onclick="pop()">
</body></html>
 
L

Lee

Christopher Benson-Manica said:
So why, then, does a reference to a variable in another window become
invalid when that window is destroyed?

Because the storage allocated for the object is freed when the
window containing it is destroyed.
 
C

Christopher Benson-Manica

Duncan Booth said:
That code works fine on Mozilla so I suspect the problem is IE specific.

Perhaps, but in the "real" version of the code, where more or less the
same chain of events occurred, Firefox also threw an 'Error'/'0'
exception. IE's message was "The callee (server [not server
application]) is not available and disappeared; all connections are
invalid. The call did not execute.", which is quite cryptic. I have
no idea why my simple test did not produce the same behavior.
When I load your file from disc, it has security zone 'my computer', but
the popup has security zone 'internet'. I suspect this cross-zone scripting
is why strange things happen. If I give the script the 'mark of the web' so
it runs in internet zone, then it gets a security violation when it tries
to open the popup.

The original code was run from the internet and popped up a window
that was presumably in the same zone, although I wouldn't put it past
IE to do something bizarre anyway.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 8
Feb 2005 14:35:18, seen in Duncan Booth
<[email protected]> posted :

AIUI, adate below is a Date Object.
date=new Date(adate);

AIUI, that calls for an implicit adate.toString() followed by parsing of
the string. In

date=new Date(+adate)

adate.toString() is not used, as + that gives NaN; evidently the +
persuades the Date Object that it really ought to be a number, its
..valueOf(); and the new Date() sees a number which it only needs to
memorise. It is therefore much faster (but that should be tested in all
browsers).

Also, with +, one always gets the right answer; but without, I get NaN
if the Object originally held a date in years -68 to +69.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top