I discovered a peculiarity in JavaScript (or perhaps my browser’s idea of it):
var s = "Hello, world";
function foo(arg)
{
console.log(arg);
console.log(this);
}
foo.call(s, s);
Running the above with Firebug console enabled, I get:
Hello, world
String { 0="H", 1="e", more...}
Why does the string automatically get turned into a weird object before becoming the this passed to foo ?
The reason I call it a weird object is because jQuery chokes on it. For example:
$.each(["one", "two", "three"], function(i, x) {
$('<p></p>').text(x) .appendTo('body'); // Works
$('<p></p>').text(this).appendTo('body'); // Doesn't work
});
thisis coerced into an object, i.e.Object("test")is internally called.Note that using strict mode indeed does return the primitive value: