By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Scope of constructed objects

P: n/a
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Christopher Benson-Manica wrote:
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?

Depends what one "expects"! Your function returns a Date object complete
with all its methods and properties, initialised to the date and time it
was created. This object won't be destroyed until your program's stopped
referring to it, but nor (perhaps confusingly) will the time and date be
automatically refreshed.
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a
Lee <RE**************@cox.net> spoke thus:
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #4

P: n/a
Christopher Benson-Manica wrote:
Lee <RE**************@cox.net> spoke thus:
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?

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

You can't have a reference to a variable. You can have a reference to an
object, and the object will exist until there is no way to reference it
from your program.

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.
Jul 23 '05 #5

P: n/a
Duncan Booth <du**********@invalid.invalid> spoke thus:
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).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #6

P: n/a
Duncan Booth <du**********@invalid.invalid> spoke thus:
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>

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #7

P: n/a
Christopher Benson-Manica wrote:
Duncan Booth <du**********@invalid.invalid> spoke thus:
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...


That code works fine on Mozilla so I suspect the problem is IE specific.

My guess, given the amount of security I had to disable to get your example
to run on IE at all, is that Microsoft think there is a possible security
violation, so once the other window has been closed they don't let you
access the object. Any attempt to do things to the date object once the
window has been closed seems to throw an exception with name 'Error' and
message '0'. The object still exists, just useful things like getting its
value are being denied.

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.

You can BTW get this specific example to work by copying the date:

function setDate( adate ) {
date=new Date(adate);
alert( date );
pwin.close();
}
Jul 23 '05 #8

P: n/a
Duncan Booth wrote:
My guess, given the amount of security I had to disable to get your
example to run on IE at all, is that Microsoft think there is a
possible security violation, so once the other window has been closed
they don't let you access the object. Any attempt to do things to the
date object once the window has been closed seems to throw an
exception with name 'Error' and message '0'. The object still exists,
just useful things like getting its value are being denied.


Thinking a bit more about this:

You can only access an object from a different window if the window has the
same source (i.e. same server and port). That means that somewhere
internally the objects created by a window have to keep a reference to the
security context which created them. I guess that when you destroy the
window Microsoft must close the security context for that window and
thereby disable all subsequent access to objects created in that context.

Jul 23 '05 #9

P: n/a
Duncan Booth <du**********@invalid.invalid> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #10

P: n/a
Lee
Christopher Benson-Manica said:

Lee <RE**************@cox.net> spoke thus:
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?


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

Jul 23 '05 #11

P: n/a
JRS: In article <Xn***************************@127.0.0.1>, dated Tue, 8
Feb 2005 14:35:18, seen in news:comp.lang.javascript, Duncan Booth
<du**********@invalid.invalid> 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.

--
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 23 '05 #12

P: n/a
Dr John Stockton wrote:
JRS: In article <Xn***************************@127.0.0.1>, dated Tue, 8
Feb 2005 14:35:18, seen in news:comp.lang.javascript, Duncan Booth
<du**********@invalid.invalid> 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.


You understand correctly.
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.


Very interesting. I had assumed, without checking at all, that Date()
objects would copy themselves correctly. I see according to the Ecmascript
standard toString() and parse() are supposed (but not required) to
roundtrip, and the constructor has to work in exactly the same way as the
parse method, but as usual in javascript its the actual implementations
that matter, not the standard.

"It is intended that for any Date value d, the result of
Date.prototype.parse(d.toString()) (15.9.4.2) is equal to d."

Thanks for that.
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.