Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript delete mechanics [duplicate]

Tags:

javascript

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

like image 284
bcurcio Avatar asked Aug 25 '26 08:08

bcurcio


1 Answers

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/


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!