为什么(function(){return this}).bind('abc')()==='abc'等于false?

oco*_*mfd 5 javascript

假设我有这样的功能:

var f1=function(){
   return this;
};
console.log(f1.bind('abc')()==='abc');
Run Code Online (Sandbox Code Playgroud)

我知道,f1.bind('abc')应该创建一个返回'abc'的新函数,所以我猜它应该输出相同的

console.log('abc'==='abc')
Run Code Online (Sandbox Code Playgroud)

这是真的,但现在输出是假的,我的猜测有什么不对?

Tus*_*har 4

在非严格模式下,原始值被包装在Object中。所以,'abc'变成new Object('abc').

在严格模式下,这种情况不会发生。

'use strict';

var f1 = function() {
  return this;
};
console.log(f1.bind('abc')() === 'abc');
Run Code Online (Sandbox Code Playgroud)

引用严格模式的 ES6 规范

如果this在严格模式代码内求值,则该this值不会被强制为对象。null或的 this 值undefined不会转换为全局对象,并且原始值不会转换为包装对象。this通过函数调用(包括使用Function.prototype.apply和进行的调用)传递的值Function.prototype.call不会将传递的 this 值强制传递给对象(9.2.1.2、19.2.3.1、19.2.3.3)。