Function.prototype.call在严格模式之外改变了typeof; 为什么?

Tor*_*ann 6 javascript function strict call strict-mode

var example = function () {
  console.log(typeof this);
  return this;
};
Run Code Online (Sandbox Code Playgroud)

在严格模式下: example.call('test') # prints 'string'

除此以外, example.call('test') # prints 'object'

但是,console.log(example.call('test'))打印test(正如您所期望的那样)

为什么Function.call变化typeof 'test' === 'string'局限于this内部example

ade*_*neo 6

当使用参数call()并将this参数设置为原始值时,该原始值始终转换为对象,因此您将获得字符串对象而不是原始字符串

String {0: "t", 1: "e", 2: "s", 3: "t", length: 4, ...
Run Code Online (Sandbox Code Playgroud)

对于文档call()MDN指出

thisArg为函数调用提供
的值this.
请注意,这可能不是方法看到的实际值:如果方法是非严格模式代码中的函数,null并且undefined将被全局对象替换,并且原始值将转换为对象.

因此,在非严格模式下,原始字符串值将转换为对象,这也在ECMA标准附录C中指定

严格模式限制和异常
如果this在严格模式代码中进行评估,则该this值不会强制转换为对象.
此值null或未undefined转换为全局对象,原始值不会转换为包装器对象.通过函数调用传递
this值(包括使用Function.prototype.apply和进行的调用Function.prototype.call)不会将传递的this值强制转换为对象