“Use strtic”
n=10;
console.log(n);
function fn(){
“Use strtic”;
n=10;
console.log(n);
}
function show() {
"use strict";
var a = uname = "刘德华";
console.log(uname);
}
报错: uname变量没有声明
非严格模式函数内的this指向window,严格模式下this指向undefine
此处补充一下几种场景下this的指向
函数内部的this指向window,严格模式下指向undefine
事件绑定的this默认指向当前被绑定的元素
this在对象的函数里默认指向当前对象
this在箭头函数中默认指向上下文对象
构造函数有显示返回值且该值是对象时,this指向这个这个对象;
如果返回的不是对象或者没有返回值时,this指向实例。
定时器内的this指向window,因为定时器方法时定义在window下的
这个不必多说
“use strict"
if (true) {
function fn4() {
console.log(111)
}
}
会报错Octal literals are not allowed in strict mode.
不使用严格模式:
function fn5(num){
num=20;
console.log(arguments[0]);
console.log(num);
}
fn5(10);//20,20
使用严格模式改变形参:
function fn5(num){
"use strict";
num=20;
console.log(arguments[0]);
console.log(num);
}
fn5(10);//10,20
使用严格模式改变arguments
function fn5(num){
"use strict";
arguments[0]=20;
console.log(arguments[0]);
console.log(num);
}
fn5(10);//20,10
原文:https://www.cnblogs.com/blogs-form-Ann/p/14780532.html