ES6严格模式

严格模式

定义严格模式的两种方式

1、“use strict”

"use strict"
// 必须放在script标签的第一行使用就变成严格模式

2. type=“module”

在script标签内定义type="module"就可以
<script type="module">

</script>

使用

1、严格模式下未使用 var定义的变量都会报错

a = 100; 
console.log(a) // Uncaught ReferenceError: a is not defined
 a = 100; 
console.log(a) // 100
// 严格模式时,顶级对象不指向window
例子
console.log(a); // f a(){}
a=3;
function a(){

}


console.log(a); // undefined
var a = 5;

2、函数的参数不能同名

function fn(a, a) {
    console.log(a, a);// Uncaught SyntaxError: Duplicate parameter name not allowed in this context
}
fn(3, 5);
案例
var a = 3;
{
    // .... b
    a = 4;
    function a() {
        console.log("a")
    }
    a = 5;
    function a() {
        console.log("b")
    }
    a = 6;
    console.log(a, "___1")  6
}
console.log(a, "___2")  5
var a = 5;
if (a < 6) {
    // a=7;
    function a() {

    }
    // a=8;
}
console.log(a); // 7  
function fn(a) {
    if (a === undefined) {
        a = "你好";
    } else {
        var a = 3;
    }
    console.log(a); // 3 如果fn()不传参就是undefined
}
fn(2);

3、不能使用with语句

var div = document.createElement("div");
div.style.width = "50px";
div.style.height = "50px";
div.style.backgroundColor = "red";
// with可以这样使用
with (div.style) {
    width = "50px";
    height = "50px";
    backgroundColor = "red";
}
var obj ={ a:{ b:{ c:1,d:2 } } }
with(obj.a.b){c=3;d=5;} 
console.log(obj) // c: 3  d: 5
// 但是
var obj = {
    a: {
        b: {
            c: 1,
            d: 2
        }
    }
}

with (obj.a.b) {
    c = 3;
    d = 5;
    e = 10;
}
console.log(obj) //  c = 3; d = 5;
// 没有e这个东西 此时e就成为了一个全局变量
console.log(e) // 10
//原始属性存在的情况下 我们才能赋值 原始属性不存在 创造一个全局变量

不能修改只读属性

var str = "aaaaa";
str.length = 10;
// Uncaught TypeError: Cannot assign to read only property 'length' of string 'aaaaa'

不能删除不可删除属性

var str = "aaaaa";
delete str.length;
var arr = [1, 2, 3];
delete arr.length;
console.log(arr.length) // Uncaught TypeError: Cannot delete property 'length' of [object Array]

不能使用前缀0八进制

var x = 046;
// Uncaught SyntaxError: Octal literals are not allowed in strict mode.

在严格模式下不能使用arguments.callee,arguments.callee.caller

在严格模式下不能使用arguments.callee,arguments.callee.caller
function fn() {
    console.log(arguments.callee)//当前函数 当需要调用到当前函数时,当前函数是匿名函数
    console.log(arguments.callee.caller);//回调当前函数的上下文环境中的函数  fn1(){fn(1,2,3);}
}
function fn1() {
    fn(1, 2, 3);

}
fn1();

document.addEventListener("click", function () {
	console.log("aa")
	document.removeEventListener("click", arguments.callee); // 点击删除事件 没有函数名 这个arguments.callee就是当前函数
})

禁止this指向全局对象

console.log(this) // 指向window
function fn(){
	console.log(this);// window  非严格模式指向window 严格模式时指向undefined
}
fn()

// 默认回调函数时 this指向window
var obj = {
	   a: function (f) {
	       f();
	   },
	   b: function () {
	       console.log(this);// window || undefined
	   }
}
obj.a(obj.b);

setTimeout(obj.b, 1000);//window
setTimeout(obj.b(), 1000);//obj

举例:我让张三帮我带饭 这是我直接给张三说的 这是直接执行的 指向的是obj
	  我让小李给张三说 让张三给我带饭 这就是回调函数 这是我让小李去执行的

知识点 反射 eval

var obj = { a: 1,b: 2}
console.log(eval("obj")) // {a: 1, b: 2} 直接就是一个对象
console.log(eval("10+30")) // 40
虽然很好用 但是不要在严格模式用eval
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值