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

using delete

P: n/a
Why is it that if I do this:

Foo = function() {
this.prop = true;
};
Foo.prototype.hi = function() {
alert("hi");
};
bar = new Foo();
delete bar;
bar.hi();

nothing gets alerted. But if I change the call to the constructor to

var bar = new Foo();

then the alert gets fired. And if I do this:

Foo = function() {
this.prop = true;
};
Foo.prototype.hi = function() {
delete this;
};
bar = new Foo();
bar.hi();

the object 'bar' does not seem to have been deleted? I'm testing with FF 2.

Andrew Poulos
Feb 2 '07 #1
Share this Question
Share on Google+
3 Replies

P: n/a
On Feb 2, 11:46 am, Andrew Poulos <ap_p...@hotmail.comwrote:
Why is it that if I do this:

Foo = function() {
this.prop = true;};

Foo.prototype.hi = function() {
alert("hi");};

bar = new Foo();
delete bar;
bar.hi();

nothing gets alerted.
But you do get an error report (you should look at/for error reports).
But if I change the call to the constructor to

var bar = new Foo();

then the alert gets fired. And if I do this:
Variables declared in function or global execution contexts are marked
with the - DontDelete - attribute and so cannot be deleted (the delete
operation evaluates as false instead of true to indicate its falure
(sort of)).
Foo = function() {
this.prop = true;};

Foo.prototype.hi = function() {
delete this;};

bar = new Foo();
bar.hi();
Why would it? You have attempted to delete - this -, which is not
possible as - this - does not evaluate as a Reference type (it is a
value that is a reference to an object), and so the delete operator
does not act at all (but the delete expression still evaluates as true
in this case).

In any event, the - delete - operator works only by removing
properties from objects (where they are not marked as - DontDelete -)
and whatever object is referred to by - this - does not know which
properties of other objects (including Variable objects) may refer to
it, if any).

Richard.

Feb 2 '07 #2

P: n/a
Richard Cornford wrote:
Variables declared in function or global execution contexts are marked
with the - DontDelete - attribute and so cannot be deleted (the delete
operation evaluates as false instead of true to indicate its falure
(sort of)).
Interesting gotcha.
Feb 4 '07 #3

P: n/a
pcx99 wrote:
Richard Cornford wrote:
>Variables declared in function or global execution contexts
are marked with the - DontDelete - attribute and so cannot
be deleted (the delete operation evaluates as false instead
of true to indicate its falure (sort of)).

Interesting gotcha.
Why is that a "gotcha"? There is no particularly good reason for wanting
to delete declared variables so not being able to is is hardly an
inconvenience.

Richard.

Feb 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.