var example = function () {
console.log(typeof this);
return this;
};
In strict mode: example.call('test') # prints 'string'
Otherwise, example.call('test') # prints 'object'
However, console.log(example.call('test'))
prints test
(as you'd expect)
Why does Function.call
change typeof 'test' === 'string'
bound to this
inside example
?
When using
call()
and setting thethis
argument to a primitive value, that primitive value is always converted to an object, so you get the string object instead of the primitive stringThe documentation for
call()
on MDN states thatSo in non-strict mode the primitive string value is converted to an object, this is also specified in the ECMA standard, Annex C