最近,我通过克罗福德 查了一些JavaScript代码JSLint JSLint,并给出了以下错误:

第1行第1字符1:缺少“严格使用”声明的问题。

在做一些搜索时,我意识到有些人加了"use strict";输入 JavaScript 代码。 一旦我添加了该语句, 错误就不再出现。 不幸的是, Google 并未披露此字符串语句背后的大部分历史。 当然, 它肯定与浏览器如何解读 JavaScript 有关, 但我不知道效果会是什么 。

那么,什么是"use strict";关于它的意义是什么,它是否仍然相关?

当前浏览器中的任意浏览器响应"use strict";字符串, 还是用于未来用途 ?


当前回答

可比较的小例子 :

无限制模式 :

for (i of [1,2,3]) console.log(i)
    
// output:
// 1
// 2
// 3

严格模式 :

'use strict';
for (i of [1,2,3]) console.log(i)

// output:
// Uncaught ReferenceError: i is not defined

无限制模式 :

String.prototype.test = function () {
  console.log(typeof this === 'string');
};

'a'.test();

// output
// false

String.prototype.test = function () {
  'use strict';
  
  console.log(typeof this === 'string');
};

'a'.test();

// output
// true

其他回答

添加时添加"use strict";中,下列情况将投语法错误在脚本执行之前:

  • 为未来的ECMAScript版本铺路,使用一个新保留的关键关键关键关键词(在ECMAScript 6): implements, interface, let, package, private, protected, public, static, 和yield.

  • 在区块中声明函数

    if(a<b){ function f(){} }
    
  • 八进语法

    var n = 023;
    
  • this点对全局对象。

     function f() {
          "use strict";
          this.a = 1;
     };
     f(); 
    
  • 在对象字典中为属性名称声明相同名称的两倍

     {a: 1, b: 3, a: 7} 
    

    ECMACcript 6 (ECMAScript 6)已不再属于这种情况(ECOMACcript 6) (EECMAScript 6) (EECMACcript 6) (EECMACcript 6) (EECMACcript 6)) 。昆虫 1041128).

  • 以相同名称函数宣告两个函数参数

    f(a, b, b){}
    
  • 设定未声明变量的值

    function f(x){
       "use strict";
       var a = 12;
       b = a + x*35; // error!
    }
    f();
    
  • 使用delete在变量名称上delete myVariable;

  • 使用evalarguments作为变量或函数参数名称

    "use strict";
    arguments++;
    var obj = { set p(arguments) { } };
    try { } catch (arguments) { }
    function arguments() { } 
    

资料来源:

如果人们担心使用use strict也许值得看看这篇文章:

在浏览器中支持 ECMAScript 5 “ 立体模式” 。 这是什么意思 ?
NovoGeeek.com - 克里希纳的博客

讨论浏览器支持, 但更重要的是如何安全处理:

function isStrictMode(){
    return !this;
} 
/*
   returns false, since 'this' refers to global object and 
   '!this' becomes false
*/

function isStrictMode(){   
    "use strict";
    return !this;
} 
/* 
   returns true, since in strict mode the keyword 'this'
   does not refer to global object, unlike traditional JS. 
   So here, 'this' is 'undefined' and '!this' becomes true.
*/

如果您使用在过去一年左右释放的浏览器, 那么它很可能支持 JavaScript 严格模式。 只有 ECMAScript 5 之前的旧浏览器才成为当前标准, 不支持它 。

命令周围的引号确保代码在旧浏览器中仍然有效(尽管那些在严格模式下产生语法错误的东西通常只会导致脚本在旧浏览器中发生一些难以探测的故障 ) 。

声明声明"use strict"; 指示浏览器使用严格模式,该模式是JavaScript的简化和安全功能集。

特征清单(并非详尽无遗)

  1. 丢弃全局变量 。var以变量名称填报声明和打字)

  2. 静音失灵任务会以严格的方式丢出错误( 指派)NaN = 5;)

  3. 试图删除不可迁移属性的尝试将会丢弃( ) 。delete Object.prototype)

  4. 要求对象字典中的所有属性名称都具有独一性( W)var x = {x1: "1", x1: "2"})

  5. 函数参数名称必须是独有的( V)function sum (x, x) {...})

  6. 禁止辛醇- 辛醇- 辛醇- 辛醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙醇- 乙酯var x = 023;有些德意志人错误地认为,前零位数没有改变数字。 )

  7. 禁止with关键字

  8. eval在严格模式下不引入新变量

  9. 禁止删除普通名称(delete x;)

  10. 强制禁止或指定姓名evalarguments以任何形式

  11. 严格模式不使用arguments对象的正格式参数。 (例如,在function sum (a,b) { return arguments[0] + b;}之所以有效,是因为arguments[0]a等) (......) (见见examples下一节,以了解差异)

  12. arguments.callee不支持不支持

[参考:严格模式, Mozilla 开发者网络]


实例:

  1. 严格模式代码不使用在其中创建的参数对象的别名属性
function show( msg ){
    msg = 42;
    console.log( msg );          // msg === 42
    console.log( arguments[0] ); // arguments === 42
}
show( "Hey" );

// In strict mode arguments[i] does not track the value of 
// the corresponding named argument, nor does a named argument track the value in the corresponding arguments[i]
function showStrict( msg ){
    "use strict";
    msg = 42;
    console.log( msg );          // msg === 42
    console.log( arguments[0] ); // arguments === "Hey"
}
showStrict( "Hey" );

严格使用用于显示常见和重复错误, 以便以不同的方式处理, 并改变 Java 脚本的运行方式,

  • 预防意外全球事故

  • 无重复

  • 消除用

  • 消除这种胁迫

  • 更安全电子( )

  • 不可更改的错误

您也可以阅读此文件第1条详细信息