Memory scope of anonymous object

V

VK

Just donged on me:

function foobar() {
foo = 'bar';
// global variable
}

function foobar() {
var foo = 'bar';
// local variable
}

function foobar() {
var foo = (new Date()).getTime();
// (new Date()) object:
// does it suppose to be like local foo
// or it will hand up somewhere in the
// global scope?
}
 
M

Matt Kruse

VK said:
function foobar() {
var foo = (new Date()).getTime();
// (new Date()) object:
// does it suppose to be like local foo
// or it will hand up somewhere in the
// global scope?
}

The date object has no references to it, so it is not in any scope beyond
the statement.
It is elligible for garbage collection after the statement is executed.
 
R

Rob Williscroft

VK wrote in in
comp.lang.javascript:
Just donged on me:

function foobar() {
foo = 'bar';
// global variable
}

Its foo that iss global, 'bar' is just a string:

var g = "a string";
function f()
{
var l = g;
}

l is at f's scope and g it at global scope, "a string" is just
refered to by both of them.
function foobar() {
var foo = 'bar';
// local variable
}

function foobar() {
var foo = (new Date()).getTime();
// (new Date()) object:
// does it suppose to be like local foo
// or it will hand up somewhere in the
// global scope?
}

The Date object should be subject to collection after the first line
of foobar(), IOW it nolonger (for all intents and purposes) exists.

just-my-two-peneth-ly yr's Rob.
 
V

VK

Matt said:
The date object has no references to it, so it is not in any scope beyond
the statement.
It is elligible for garbage collection after the statement is executed.

Thank you.

This a bit paranoidal question was influensed by the var-less variable
declaration in functions: they are going into the global scope. Like
this "i":

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function foo() {
i = 0;
}
</script>
</head>

<body bgcolor="#FFFFFF" onload="foo();">
<button type="button" onclick="alert(i)">Test</button>
</body>
</html>

So (new Date()) is creating a new object instance but w/o var operator,
so I thought hell knows if it shares the same behavior as "i" above.
But as an ECMA-ignorant I can be totally out of the loop here. Thinking
back that "globalization" applies only to the left side of the
assignment (?).
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top