JS的严格模式和标准模式

严格模式的好处:

规避了代码中的一些不严谨,不规范的用法,提高了编译器的运行效率,为后续版本做铺垫

用法:在当前环境顶部(全局,局部环境)加上两个单词:“use strict”
"use strict"        //添加严格模式

function foo() {
"use static"        //添加严格模式
    }
严格模式下,不能使用为未声明的变量:
    "use strict"
    x = 123;
    console.log(x); // x is not defined
	// 非严格模式

   x=123;     
   console.log(x)     //输出:123
严格模式下,函数不允许参数名相同
//非严格模式
function a(a, a) {
    console.log(a, a)
}
a(12, 34) // 34,34
//严格模式
"use strict"

function a(a, a) {
    console.log(a, a)
}
a(12, 34) // Duplicate parameter name not allowed in this context
严格模式不允许使用八进制
"use strict"
var num = 017;  
console.log(num)    // Octal literals are not allowed in strict mode.
严格模式下,变量名不能使用 “eval” 字符串
"use strict"
var eval = 1234;
console.log(eval);  // Unexpected eval or arguments in strict mode
// 非严格模式
var eval = 1234;
console.log(eval);  //1234
严格模式下,变量名不能使用 “arguments” 字符串
// 严格模式
"use strict"
var arguments = 1234;
console.log(arguments);     // Unexpected eval or arguments in strict mode
// 非严格模式
var arguments = 1234;
console.log(arguments);     // 1234
严格模式下,不允许使用with语句
"use strict"
var person = {
    name: "小明",
    age: 20,
};
with(person) {
    console.log(name, age); //Strict mode code may not include a with statement
}
//非严格模式
var person = {
    name: "小明",
    age: 20,
};
with(person) {
    console.log(name, age); // 小明 20
}
arguments不追踪参数变化
"use strict"

function foo(x) {
    x = 1;
    console.log(arguments[0]);      // 10
}
foo(10);        
// 非严格模式
function foo(x) {
    x = 1;
    console.log(arguments[0]);  //1
}
foo(10);
不能用delete删除一个变量
 "use strtic"
 var a = 10;
 delete a;
 console.log(a); // 不能使用delete删除一个变量 如果不使用var则可以删除
 // 严格模式下不带var 和 let 不能声明变量
函数this问题
"use strict"
  function foo() {
            console.log(this); //undefined
        }
        foo(); //不知道是谁调用foo
        window.foo();
设立eval作用域
      "use strict"
      eval("var a = 10;console.log(a)");
      console.log(a); //a 是访问不到
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值