且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除非限定标识符时,严格模式语法错误背后的动机是什么?

更新时间:2023-11-20 19:05:10

您在谈论第11.4.1节,第5.a.段的规格:



  1. 否则,ref是对环境记录绑定的引用,所以

    a。如果IsStrictReference(ref)为true,则抛出一个SyntaxError异常。

    b。让绑定成为GetBase(ref)。

    c。返回调用DeleteBinding具体绑定方法的结果,提供GetReferencedName(ref)作为参数。


你所谓的非限定标识符正式命名为环境记录绑定。

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

现在,问你的问题。为什么在5.c.时抛出一个SyntaxError。反正会失败吗?我想你自己回答了这个问题!

Now, to your question. Why throw a SyntaxError when 5.c. would fail anyway? I think you answered it yourself!


严格模式必须在这里进行运行时检查,因为抛出了TypeError遇到这种情况时

Strict mode must do a runtime check here, because a TypeError is thrown when this is encountered.

这是对的。但快速失败总是更好。因此,当有可能检测到SyntaxError(解析时间)时,应该采取这种机会。

That's right. But it's always better to fail fast. So, when there is a chance of detecting a SyntaxError (at parse time), that opportunity should be taken.

为什么?如果发生错误,它可以帮您省去修复应用程序的麻烦。考虑可能会立即向您显示错误的IDE,而不是几小时的调试。

此外,这些限制可能对优化的JIT编译器有利。

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.