1

Possible Duplicate:
Deleting Objects in JavaScript

I have came to the use of delete property. It isn't working. I don't know whether it is a browser compatibilty issue or not? is it a reference being not deleted?

My concepts about pointer is not that clear. Please let us know whats going wrong?

Code:

    <html>
        <script language="JavaScript">
        MyDate = new Date();
        document.write("MyDate=",MyDate,"<br>");

        delete MyDate;
        document.write("MyDate=",MyDate);   //MyDate=undefined should be output
        </script>
    </html>

But the output is only first document.write:

MyDate=Mon Oct 01 2012 15:27:56 GMT+0530 (India Standard Time)
Community
  • 1
  • 1
V.J.
  • 918
  • 3
  • 18
  • 37

3 Answers3

1

IIRC, delete in javascript deletes properties.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • Yes, this is the proper behaviour of `delete`. However, in the browser, all global variables are properties of the `window` object and are deletable. – Andy E Oct 01 '12 at 10:05
  • @AndyE, that should be `delete window.MyDate` then, I believe. – Michael Krelin - hacker Oct 01 '12 at 10:10
  • technically, yes. However, some browsers may treat `window` as implicit when not running code in strict mode. See @Shusl's answer for a jsfiddle example. – Andy E Oct 01 '12 at 10:22
  • Interesting. When trying that from console, it doesn't work in my chrome. In jsfiddle, on the other hand, it does, meaning it isn't running in exactly same context. Seems like relying on implicit context makes things very fragile and is probably to be avoided. – Michael Krelin - hacker Oct 01 '12 at 10:24
  • Yes, Chrome's console and most other consoles are a very fancy `eval`, which changes a lot of rules :-) See kangax's [*Understanding Delete*](http://perfectionkills.com/understanding-delete) article for more info on `eval` and `delete`. As an amendment to my earlier statement, it appears most browsers do allow deleting normal variables and not just where `window` would be implicit. Strict mode, however, will throw an error. – Andy E Oct 01 '12 at 10:27
  • 1
    Interesting again ;-). But, it seems, the OP has one of those browsers that object to this treatment. Hell, you do know a thing or two, you should've braindumped into the answer of your own. I'm not master of javascript, just answered, because there were no other answers :) – Michael Krelin - hacker Oct 01 '12 at 10:35
  • 1
    @AndyE: Slightly wrong: global variables declared with `var` are not deletable from the global object (per spec, at least: I think old IE gets this wrong). The kangax article has the details. – Tim Down Oct 01 '12 at 11:35
  • @Tim: yes, you're right. It's the old IE behaviour that confused me here, since I used to write code for it exclusively (it's also a while since I read kangax's article!). – Andy E Oct 01 '12 at 13:25
1

It is working . since you are deleting the myDate, browser is throwing an exception while you trying to access myDate in second document.write jsfiddle

Uncaught ReferenceError: MyDate is not defined 
Anoop
  • 23,044
  • 10
  • 62
  • 76
1

First off, that's not how delete is supposed to be used. You can use it to delete properties from an object, so in your case you should write delete window.MyDate

Why should it be undefined? You remove the variable, thus you get

Error: ReferenceError: MyDate is not defined

Prinzhorn
  • 22,120
  • 7
  • 61
  • 65