高级前端
js
【Q569】关于 this 与包装对象,以下输出多少

关于 this 与包装对象,以下输出多少

更多描述

function foo() {
  console.log(this);
}
 
foo.call(3);

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 584 (opens in a new tab)

Author 回答者: mrrs878 (opens in a new tab)

如果处于非严格模式下,要绑定的this指定为nullundefined时会自动替换为全局对象,原始值则会被包装

严格模式:

"use strict";
 
function test() {
  console.log(this);
}
 
test.call(2);
// 2

非严格模式

function test() {
  console.log(this);
}
test.call(2);
// Number {2}