What is the purpose of the delete operator in Javascript?

后端 未结 6 1008
一向
一向 2020-12-04 14:22

The behaviour of the delete operator seems very complicated and there are many misunderstandings about what it actually does. To me, it seems that reassigning something to <

6条回答
  •  隐瞒了意图╮
    2020-12-04 14:33

    You can check the answer of the following link Can I set variables to undefined or pass undefined as an argument? which explains the difference in a very detailed way.

    Summary:

    You can certainly assign undefined to it, but that won't delete the variable. Only the delete object.property operator really removes things.

    delete is really meant for properties rather than variables as such. Browsers will let you get away with straight delete variable, but it's not a good idea and won't work in ECMAScript Fifth Edition's strict mode. If you want to free up a reference to something so it can be garbage-collected, it would be more usual to say variable= null.

提交回复
热议问题