资讯专栏INFORMATION COLUMN

Javascript 严格模式特点

darry / 1558人阅读

以下是严格模式中需要注意的用法,这里需要强调的是: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 "#"
  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
  }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/97466.html

相关文章

  • javascript-函数表达式

    摘要:函数表达式定义函数表达式区别于函数声明,也是一种定义函数的方式,形似与变量赋值,这个值就是函数体,例如函数表达式之匿名函数函数表达式之具名函数匿名函数之立即执行函数目前知道的是这三种形式,希望高人补充特点区别于函数声明,和普通变量一样使用前 函数表达式 定义:函数表达式区别于函数声明,也是一种定义函数的方式,形似与变量赋值,这个值就是函数体,例如: var a = function...

    bingchen 评论0 收藏0
  • 1.你不知道的JavaScript-严格模式

    摘要:针对单个函数将放在函数体的第一行,则整个函数以严格模式运行。严格模式禁止这种用法,全局变量必须显式声明。严格模式下,这属于语法错误。严格模式禁止这种表示法,整数第一位为,将报错。也就是说,不允许在非函数的代码块内声明函数。 本文转自【阮一峰博客】:http://www.ruanyifeng.com/blo... 一、概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:...

    FuisonDesign 评论0 收藏0
  • 【前端面试】变量和类型计算

    摘要:题目使用能得到哪些类型和的选择中有哪些内置函数变量按存储方式分为哪些类型,并描述其特点如何理解知识点值类型和引用类型值类型引用类型对象,数组,函数值类型直接把值存储在堆中,把赋值给在内存中是又给开辟了一块新的空间,存储了同样的值。 1.题目 1.JS使用typeof能得到哪些类型 === 和 == 的选择 JS中有哪些内置函数 JS变量按存储方式分为哪些类型,并描述其特点 如何理解J...

    DoINsiSt 评论0 收藏0
  • JavaScript中的函数

    摘要:然后最后一步就是从父作用域链中将该特殊对象删除,整个过程的伪代码如下注意这里,该属性不能删除,只读。 起因是我在逛sf的时候看到了一个人的提问: 为什么将函数c赋值给变量b,在函数体里面,给c赋值,为什么会失败?也就是这代码执行时为什么c打印出来的不是3 var b = function c () { a=1, b=2, c=3; console.log(a); ...

    coolpail 评论0 收藏0

发表评论

0条评论

darry

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<