Javascript 严厉形式特性

以下是严厉形式中须要注重的用法,这里须要强调的是: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
    原文地址: https://segmentfault.com/a/1190000016286415
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞