Why does (function() { return this; }).call('string literal') return [String: 'string literal'] instead of 'string literal'?
.call
internally invokes [[Call]]
, which performs
- Perform OrdinaryCallBindThis(F, calleeContext, thisArgument).
And OrdinaryCallBindThis
(which sets the this
value of the function that's going to be called) will wrap the new this
in an object in non-strict mode:
If thisMode is strict, let thisValue be thisArgument.
Else,
a. If thisArgument is undefined or null, then ...
b. Else, Let thisValue be ! ToObject(thisArgument).
In strict mode, you'll see that the string does not get wrapped in an object:
'use strict';
(function() {
console.log(this);
console.log(this === 'string literal');
}).call('string literal');
Since you are calling a method on the string primitive, it is automatically converted to a String Object.
The logs are how the two consoles render String Objects.
Further reading: What is the difference between string primitives and String objects in JavaScript?