Motive behind strict mode syntax error when deleting an unqualified identifier?

后端 未结 2 1625
慢半拍i
慢半拍i 2020-12-24 15:22

I\'m having trouble understanding why, in strict mode, a syntax error occurs when delete is used on an unqualified identifier.

In most cases, it makes s

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 15:39

    If you want to delete object in strict mode. You have to explicitly mention about the property access. Also note that, how you call the function is important. If new operator isn't used this is undefined under use strict, and you cant use the below method. Example:

    'use strict'
    function func(){
      var self = this;
      self.obj = {};
      self.obj.x = 'y'
    
      console.log(self.obj);
      delete self.obj // works
      // delete obj // doesn't work
      console.log(self.obj);
    }
    
    var f = new func();
    

    For deleting object outside of the function(closure), you will have to call like

    // same code as above
    delete f.obj
    

提交回复
热议问题