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

Memory scope of anonymous object

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

Dec 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VK wrote in news:11**********************@f14g2000cwb.googlegr oups.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Dec 18 '05 #2

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

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 18 '05 #3

P: n/a
VK

Matt Kruse wrote:
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 (?).

Dec 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.