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

Deleting a Variable

P: n/a
Hi all -

Is there a way to completely destroy a javascript variable? I know of
reloading the page, using

variablex = null

and

delete variablex

but both of these methods seem to only free up the memory that the
variable was using, not destroy it completely. Any help would be
greatly appreciated. Thanks in advance.

Jan 18 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
If it rids it out of memory why does it matter anymore, to the system
its essentially gone, what are you using this for, if you don't mind me
asking? Also I've never heard of delete, what is it?

Jan 18 '06 #2

P: n/a
"M B HONG 20" <ch************@gmail.com> writes:
Is there a way to completely destroy a javascript variable?
What do you mean by "destroy"? It's not a concept use in the
language specification.
I know of reloading the page, using

variablex = null

and

delete variablex
Reloading the page should make the global object of that page
unavailable, unless another page has a reference to it (like a page
opened by javascript having a reference in the "opener" property). The
new page will have a completely new global object where a variable of
that name might not have been declared yet ... but that's hardly
destroying the variable.

Assigning a null value to a variable will only do that - change the value
of the variable. It will not change whether the variable exists or not,
and will in fact create one if it doesn't exist already.

Using the "delete" operator on a declared variable, i.e., one introduced
with the "var" keyword, will not work, since the variable is created with
the attribute "DONTDELETE".
but both of these methods seem to only free up the memory that the
variable was using, not destroy it completely. Any help would be
greatly appreciated.


If a property of the global object has the attribute DONTDELETE, then
you can't delete them.

Why do you need to do so?
/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.'
Jan 18 '06 #3

P: n/a
M B HONG 20 wrote:
Hi all -

Is there a way to completely destroy a javascript variable? I know of
reloading the page, using

variablex = null

and

delete variablex

but both of these methods seem to only free up the memory that the
variable was using, not destroy it completely. Any help would be
greatly appreciated. Thanks in advance.


Assign undefined to it:

<script type="text/javascript">

var x = 'blah';
alert('x is: ' + x
+ '\ntypeof x: ' + typeof x);

x = undefined;
alert('x is: ' + x
+ '\ntypeof x: ' + typeof x);

</script>

In the above, x is declared as a property of the global object and given
a value that makes it type 'string'. It is then assigned the value of
undefined which converts it to type 'undefined' - a special (primitive)
type that only has one value, undefined.

At this point, it is likely indistinguishable from a variable that
either hasn't been declared or has been declared but not given a value.
Does that fit with your concept of 'destroyed'?
--
Rob
Jan 18 '06 #4

P: n/a
Thanks to all of you for your responses.

I guess I should have been a bit more clear in my explanation. I
basically have a main window that controls child windows through
javascript variables and window.open, like so:

var newWindow;

newWindow = window.open("NewPage.aspx",
"NewPage","width=820,height=375,scrollbars=no,top= 100,left=60,location=no,dependent=yes,status=no,fu llscreen=no,toolbar=no,resizeable=no");

and I have a loop in the main page detecting that the child window is
indeed open like so:

if (newWindow)
{
if (!newWindow.closed)
{
//some action
}
else if (newWindow.closed)
{
//some action
}
}

I noticed that on the first load of the window (the variable
'newWindow') the loop in the main page detects it right away, but after
the variable has already been set, the loop only detects the page after
the content in the page has loaded, meaning there is a lag between the
request of the child page and when the main page detects it in the
loop. I thought that destroying the variable 'newWindow' might help,
but I don't know if such a thing exists, and i have tried all of your
advice but none seem to work for my specific problem...

Jan 18 '06 #5

P: n/a
RobG wrote:
M B HONG 20 wrote:
Is there a way to completely destroy a javascript variable? I know of
reloading the page, using

variablex = null

and

delete variablex

but both of these methods seem to only free up the memory that the
variable was using, not destroy it completely. Any help would be
greatly appreciated. Thanks in advance.


Assign undefined to it:

<script type="text/javascript">

var x = 'blah';
alert('x is: ' + x
+ '\ntypeof x: ' + typeof x);

x = undefined;
[...]


Since this could fail if the `undefined' property of the Global Object is
not supported, one may want to assign

var undefined = this.undefined;

and the like before. Looks strange (and may trigger a warning) but works
always (unless there is indeed an `undefined' property of the caller with
a value different to `undefined').
PointedEars
Feb 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.