strict 严格模式

171 阅读1分钟

严格模式下的this 是undefined 严格模式禁止自定义函数的this指向window

babel 在转换jsx时使用了严格模式,可以把代码拷贝到babel官网调试看下

类中的属性方法

默认开启了严格模式

function demo {
    console.log(this);  //window
}

function demo1 {
 'use strict'
    console.log(this) //undefined
}

setTimeOut 非严格模式下 this 指向 window ; 严格模式下 this 是undefined

function test() {
    setTimeout(()=>{console.log(this,'非严格模式this-----')},0)
}
    
function test1() { 
   'use strict';
    setTimeout(()=>{console.log(this,'严格模式下this-----')},0)
}