Javascript 在严格模式下禁止指向 this

时间:2023-03-09 16:55:11
Javascript 在严格模式下禁止指向 this

如下代码, f() 输出的是 false,而 f2() 输出的是 true。

这是因为 f2 在严格模式下禁止 this 指向全局,所以 this 是 undefined, !this 当然是 true。

    <script>
function f(){
console.log(!this);
} function f2(){
"use strict";
console.log(!this);
} f();
f2();
</script>