严格模式“use strict“

这次要分享的是严格模式,严格模式并不是ES6才出现的,在ES5中也有严格模式,严格模式指的是融合了ES5和ES6的严格模式。

“use strict”;//定义在script标签的最上方

<script type="module>//或者可以给script添加一个type属性,属性值为module;

(1)任何变量都必须先声明,不能直接使用,否则会报错
非严格模式下,可以直接对变量赋值,不用先使用var let等定义变量

(2)参数名不能重复
function fn(a,a){
console.log(a,a);
}
fn(1,2);

(3)不允许使用with
通常情况下:
var div=document.querySelector(“div”);
div.style.width = “50px”;
div.style.height= “50px”;
div.style.backgroundColor = “red”;
使用with时
with(div.style){
width = “50px”;
height = “50px”;
backgroundColor = “red”;
}

(4)只读属性不能修改
var str = “abcdefg”;
str.length=5;

(5)不允许使用8进制
var a=034;

(6)不可删除固有属性
var arr = [1,2,3];
delete arr.length;

(7)不能使用callee和caller
function fn(){
console.log(arguments.callee);
console.log(arguments.callee.caller);
}
(8)全局函数中尽量不要使用this
function fn(){
console.log(this);
}
没有开启严格模式时,this指向时window,
但是开启严格模式后,函数中this就会变成undefined
(9)增加保留字
比如protected、static和interface
定义变量时不能使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值