English 中文(简体)
在删除无保留识别标志时,恪守严格的同义词错误?
原标题:Motive behind strict mode syntax error when deleting an unqualified identifier?

我担心的是,为什么严格地说,在<代码>delete上使用无保留识别码时,会发生yn误。

在多数情况下,如果你以通常的方式宣布与<代码>var关键词的变数,然后试图以无限制的方式使用<代码>delete,这种变数将无谓地失败,那么,严格方式避免在这些情况下出现错误。

However, there are cases where you can t delete identifiers that are qualified:

(function() {

  // "use strict";

  var obj = Object.create({}, { bloop: { configurable: false } });

  delete obj.bloop; // throws TypeError in strict mode, silently fails in non-strict.

  console.log( bloop  in obj); // true

}());

严格控制方式必须在这里进行时间检查,因为遇到这种情况时,就会扔下一种类型。 在某些情况下,你can成功删除了非限制性方式中的无保留识别符号......

// "use strict";

window.bar = 6;

console.log(typeof bar); // number

delete bar; // works in non-strict, syntax error in strict!

console.log(typeof bar); // undefined

In fact, to my understanding, whether or not you can delete things (in non-strict mode) depends on the internal [[Configurable]] property, and has nothing to do with qualified identifiers. As far as I can tell, there is no way in strict mode to delete non-global variables that (as properties of the local VO) are configurable:

(function() {

  // "use strict";

  eval( var foo = 5; );

  console.log(typeof foo); // number

  delete foo; // works in non-strict, SyntaxError in strict.

  console.log(typeof foo); // undefined

}());

因此,我的问题是,在使用<代码>delete<>/code>时,如果财产不可以证明,那么类型Error会扔下任何东西,那么在使用“<代码>delete<>/code>时,究竟是怎样? 这似乎是一种不必要的限制,在某些情况下,除了不采用严格方式外,似乎还没有任何工作(第三例)。 谁能解释这一决定背后的动力?


最新情况:我刚刚认识到,我忽略了以下事实:直接的<代码>eval。 电话具有严格的范围,而不是电话功能的范围,例如第3条:<代码>foo 定义不严格。 无论如何,操作时间检查仍会达到这一目的,但它提出了一个附带问题: 我们没有办法严格地具有可塑性的地方变数:<代码>eval。 d 非限制性的变式声明? AFAIK是<代码>eval少数合法用途之一。

最佳回答

You are talking about Section 11.4.1, paragraph 5.a. of the specs:

  1. Else, ref is a Reference to an Environment Record binding, so
    a. If IsStrictReference(ref) is true, throw a SyntaxError exception.
    b. Let bindings be GetBase(ref).
    c. Return the result of calling the DeleteBinding concrete method of bindings, providing GetReferencedName(ref) as the argument.

What you called "unqualified identifiers" is officially named "Environment Record binding".

现在,对你的问题。 为什么在5.c.时扔进一个SyntaxError。 失败了吗? 我想你们回答了!

严格方式必须在这里进行<>定期检查,因为当碰到时,就会有人投掷

这一权利。 但是,失败的速度总是较好。 因此,当有机会发现SyntaxError(parse time)时,应当利用这一机会。

Why? It saves you the trouble of fixing your app if an error occurs. Think about IDEs that may show you the error right away, as opposed to hours of debugging.
Also, such restrictions may be advantageous for optimized JIT compilers.

问题回答

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();

为了在职能之外删除物体,你必须呼吁

// same code as above
delete f.obj




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.