Delete: can anyone explain this behavior?

Compare this code 1 :

  somevar = 5;
  delete window.somevar;
  alert(typeof somevar) //=> undefined, so deleted

to this code:

  var somevar = 5;
  delete window.somevar;
  alert(typeof somevar) //=> number, so NOT deleted

See here in action

Now it is somevardeleted in the first block , but not in the second. The only difference is the use of the keyword varin the second block. Both units operate in a global area.

Can this be explained?

1 code cannot be tested in chrome console or firebug, and not in jsfiddle. In these environments, all the codeevalled, but in the programmed code,deleteworks on everything that is the resulteval(see more on this ). In IE <9delete window[anything]it is not allowed in any case.

+3
1

, , , (window, ) , , .

someVar window. delete.

someVarwindow. , ( , ). , var ( , ).

(, , , .)

, . (, ) - ( ), . , , , , , .

, , . , :

  • this, , ( this , ).
  • .
  • .
  • .
  • , , , .
  • arguments , - .
  • , , , .
  • , var ( ) , undefined.
  • , , .
  • ().

... . var (, var a = 5;, var a;) (a = 5;) .

, , " ", , , . var ( ..) .

. , a , , , . , a, ; , , ; , . ( ).

? , , . ():

  • .
  • .
  • .
  • this, ; .
  • , ( , , window , ).

... 8 :

  • , var ( ), binding/global, undefined.
  • , binding/global, .
  • binding/global ( . ).

... ( var ).

+10

All Articles