假设我有这样的功能:
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)
这是真的,但现在输出是假的,我的猜测有什么不对?
在非严格模式下,原始值被包装在Object中。所以,'abc'变成new Object('abc').
在严格模式下,这种情况不会发生。
'use strict';
var f1 = function() {
return this;
};
console.log(f1.bind('abc')() === 'abc');Run Code Online (Sandbox Code Playgroud)
如果
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)。