Probably irrelevant from a production standpoint, but I'd like to know why this behaves the way it does. The string literal gets interpreted as an object.
function fancyCallback(callback) {
callback(this);
console.log(typeof this); // just to see it really is an object
}
fancyCallback.call('string here', console.log);
I have to call
this.toString()
inside the function if I want the expected output. I know strings are objects in javascript (which is lovely) but in a simple console.log('abc'), they are naturally interpreted as strings. Why is that? Is this useful in any way? Please ignore the fact that fancyCallback is defined in the global scope!
console.log()simply calls the toString method on the string(function() {return this;}).call('string here')==='string here'Maybe the object was modified in some way.console.log(this)to his function, Chrome displayedString {0: "s", 1: "t", 2: "r", 3: "i", 4: "n", 5: "g", 6: " ", 7: "h", 8: "e", 9: "r", 10: "e", length: 11, formatUnicorn: function, truncate: function, splitOnLast: function, contains: function}.