以下是严格模式中需要注意的用法,这里需要强调的是:ES6 的 class 和 模块内都是默认的严格模式。其实,js 开发也会逐步走向严格模式,这应该是个趋势。

添加了保留字 protected,static 和 interface

在严格模式下,不可以用with()

(function(){
  //非严格模式
  var a = {name: "Bob"};
  with(a){
    name = "Lily";
  }
  console.log(a.name);  //Lily
})();

(function(){
  "use strict";   //严格模式
  var a = {name: "Bob"}, b = {};
  with(a, b){    //SyntaxError: Strict mode code may not include a with statement
    name = "Lily";
  }
  console.log(a.name);
})();

在严格模式下,变量必须显示声明(var/let/const)

(function(){
  //非严格模式
  a = 10;
  console.log(a);  //10
})();

(function(){
  "use strict";   //严格模式
  b = 10;   //ReferenceError: b is not defined
  console.log(b);
})();

在严格模式下,this默认是undefined

(function(){
  //非严格模式
  console.log(this);  //window
})();

(function(){
  "use strict";   //严格模式
  console.log(this);    //undefined
})();

在严格模式下,为只读变量和不可扩展对象赋值会报错, 而不是静默失败

(function(){
  //非严格模式
  var o = {
    name: "Lily"
  };
  Object.freeze(o);
  o.name = "Bob";
  o.age = 20;
  console.log(o);  //Object {name: "Bob"}
})();

(function(){
  "use strict";   //严格模式
    var o = {
    name: "Lily"
  };
  Object.freeze(o);
  o.name = "Bob";  //TypeError: Cannot assign to read only property 'name' of object '#<Object>'
  o.age = 20;  //TypeError: Can't add property age, object is not extensible
  console.log(o);
})();

在严格模式下,不可以在eval参数中定义变量和函数

(function(){
  //非严格模式
  var str1 = "var name='Lily';";
  var str2 = "function fun1(){console.log('hello');}";
  eval(str1);   //这个name定义在了全局,而不是函数内
  eval(str2);
  console.log(name);   //Lily
  fun1();    //hello
})();

(function(){
  "use strict";   //严格模式
  var str1 = "var alias='Lily';";
  var str2 = "function fun2(){console.log('hello');}";
  eval(str1);
  eval(str2);
  eval("name = 'Bob'");    //修改全局变量name
  console.log(name);   //Bob
  console.log(alias);    //ReferenceError: alias is not defined
  fun2();    //ReferenceError: fun is not defined
})();

在严格模式下,有名参数是arguments参数的静态副本,而非引用。

(function(){
  //非严格模式
  var name = "Bob";
  test(name);

  function test(alias){
    alias = "Lily";
    console.log(alias);  //Lily
    console.log(arguments[0]);  //Lily
  }
})();

(function(){
  "use strict";   //严格模式
  var name = "Bob";
  test(name);

  function test(alias){
    alias = "Lily";
    console.log(alias);  //Lily
    console.log(arguments[0]);  //Bob
  }
})();

在严格模式下,用delete删除var声明的变量和不可配置属性时抛出异常,而不是静默失败(返回false)

(function(){
  //非严格模式
  var a = 10;
  var fun = function(){console.log("fun called");};
  var o = Object.defineProperty({}, "name", {
    value: "Bob"
  });   //默认即不可配置
  delete a;   //false
  console.log(a);  //10

  delete fun;   //false
  fun();  //fun called

  delete o.name;   //false
  console.log(o.name);  //Bob

  //删除一个不存在的变量
  delete no;  //false

})();

(function(){
  "use strict";   //严格模式
  var a = 10;
  var fun = function(){console.log("fun called");};
  var o = Object.defineProperty({}, "name", {
    value: "Bob"
  });   //默认即不可配置
  //delete a;   //SyntaxError: Delete of an unqualified identifier in strict mode.
  console.log(a);

  delete fun;  //SyntaxError: Delete of an unqualified identifier in strict mode.
  fun();

  delete o.name;  //SyntaxError: Delete of an unqualified identifier in strict mode.
  console.log(o.name);

  //删除一个不存在的变量
  delete no;  //SyntaxError: Delete of an unqualified identifier in strict mode.

})();

在严格模式下,arguments和eval是关键字,不能被修改

(function(){
  //非严格模式
  eval = 10;
  eval("console.log('hello');");  //TypeError: eval is not a function

  (function(){
    arguments = 20;
    console.log(arguments);   //20
  }());

})();

(function(){
  "use strict";   //严格模式
  eval = 10;  //SyntaxError: Unexpected eval or arguments in strict mode
  eval("console.log('hello');");

  (function(){
  arguments =20;  //SyntaxError: Unexpected eval or arguments in strict mode
    console.log(arguments);
  }());

})();

在严格模式下,不可以用8进制

(function(){
  //非严格模式
  console.log(070);  //56 (因浏览器而异)
})();

(function(){
  "use strict";   //严格模式
  console.log(070);  //SyntaxError: Octal literals are not allowed in strict mode.
})();

在严格模式下,函数的形参不可以同名

(function(){
  //非严格模式
  var one = 1;
  var two = 2;
  fun(one, two);   //2
  function fun(a,a){
    console.log(a);
  }
})();

(function(){
  "use strict";   //严格模式
  var one = 1;
  var two = 2;
  fun(one, two);
  function fun(a,a){  //SyntaxError: Duplicate parameter name not allowed in this context
    console.log(a);
  }
})();

在严格模式下,不可以使用caller和arguments的属性,会报错

(function(){
  //非严格模式
  A();

  function A(){
    B();
  }
  function B(){
    console.log(B.caller);   //function A(){ B(); }
    console.log(arguments.callee);    //function B(){ console.log(B.caller); console.log(arguments.callee); }
  }
})();

(function(){
  "use strict";   //严格模式
  A();

  function A(){
    B();
  }
  function B(){
    console.log(B.caller);   //TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
    console.log(arguments.callee);    //TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
  }

Faremax
1.7k 声望705 粉丝