3

Possible Duplicate:
the delete operator in javascript

I have the following code. I don't understand why the second delete fails.

Also, I noticed that the foo function still exists even after I assigned something else to foo.

Is there a way to reference the function?
(suppose I'd want a bar2=foo() to behave like the bar assignment).

> function foo(){var bar=0; return function(){return bar++;}}
undefined
> bar = foo()
function () {return bar++;}
> bar()
0
> bar()
1
> delete bar
true
> foo = foo()
function () {return bar++;}
> foo()
0
> foo()
1
> delete foo
false

Thanks

Community
  • 1
  • 1
bcurcio
  • 548
  • 2
  • 10
  • 1
    On my side `delete foo` returned `true`. Can't reproduce your error. I runed code like: `function foo(){var bar=0; return function(){return bar++;}} bar = foo(); bar(); bar(); delete bar; foo = foo(); foo(); foo(); delete foo;` – Vyacheslav Voronchuk Nov 18 '12 at 12:02
  • I was using javascript console. I'm reading from http://perfectionkills.com/understanding-delete/ that it might the problem. – bcurcio Nov 18 '12 at 12:18
  • 1
    @VyacheslavVoronchuk http://jsfiddle.net/Sxnaw/4/ The output is `true`, `false` which is in line with what the OP described. – Asad Saeeduddin Nov 18 '12 at 12:19
  • It was trouble in Firebug (delete worked fine there), your Fiddle works as intended. – Vyacheslav Voronchuk Nov 18 '12 at 12:21

1 Answers1

7

delete only works on deleteable properties. Functions declared like this:

function f(){
}

are not deleteable.

Try using this syntax for the original function declaration:

foo = function (){var bar=0; return function(){return bar++;}}

See it here: http://jsfiddle.net/Sxnaw/

You can go through this article for an in depth explanation of deleteable and non deleteable properties: http://perfectionkills.com/understanding-delete/

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • What's the difference between `function f() {}` in scope of `window` and `window.f = function() {}`? – Vyacheslav Voronchuk Nov 18 '12 at 12:07
  • 1
    @VyacheslavVoronchuk the first is defined at parse time, the second at runtime. See [this answer](http://stackoverflow.com/a/336868/247441). – Anton Strogonoff Nov 18 '12 at 12:09
  • @VyacheslavVoronchuk One is defined as a property of the global with the `DontDelete` attribute, the other is created as a property of the global without the `DontDelete` attribute (since it is explicitly created via property assignment). You can read up more on it [here](http://perfectionkills.com/understanding-delete/) – Asad Saeeduddin Nov 18 '12 at 12:12
  • @AntonStrogonoff That is true, but it is not the key factor here. – Asad Saeeduddin Nov 18 '12 at 12:17
  • 2Asad, I upvoted your answer, it was trouble on my side, not your answer. – Vyacheslav Voronchuk Nov 18 '12 at 12:23
  • @Asad Thanks for correction, you're right. Parse-time vs. runtime definition does not determine the ability to be deleted. Linked article is nice, too. – Anton Strogonoff Nov 18 '12 at 12:29
  • Thanks Asad, it's a little clearer now. The question that remains is, shouldn't the DontDelete attribute be removed when I do a new assignment? – bcurcio Nov 18 '12 at 12:33
  • 1
    @user1586156 No, because the attribute is set when the property is *created*, not when its value is changed. You created the property in a way that makes it non deleteable, and non deleteable it shall stay, regardless of what syntax you use to change its value. – Asad Saeeduddin Nov 18 '12 at 12:35