Here is my latest discovery while experimenting with JS:
(function() { return this; }).call('string literal');
// => [String: 'string literal'] in V8
// => String { "string literal" } in FF
I stumbled on this while doing the following:
(function() { return this === 'string literal'; }).call('string literal');
// => false
Can anyone tell me why this
inside the function is not exactly what was passed as the first argument to call
?
Edit 1
What is the difference between string primitives and String objects in JavaScript? has been marked as a possible duplicate of my question.
While the answers to both questions are related to how JS wraps primitives inside Objects, I believe the questions, and their answers, are not identical.
My question was "why are the argument passed to call and the value of this inside the function different?", while the other question was "why is code block 1 faster than code block 2?"
The correct answer to my question was "because you did not use strict mode" while the answer to the other question was related to how fast data is accessed internally by engines implementing ECMAScript.
I hope this clarification is correct 😅
Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid. Note the use of const , because from the function I'm returning a string literal, a string defined in double quotes, which is a constant.
You can safely return C strings from a function in at least these ways: const char* to a string literal. It can't be modified and must not be freed by caller.
.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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With