If I understand correctly, d will contain the string literal "foo", not an object reference. However, the JavaScript engine will effectively apply the literal to the String instance when necessary, so you can call the String.prototype methods in string literals:
"some string".toUpperCase(); //Method of String.prototype
The following snippet from MDN can help explain it further (highlighted by me):
String literals (indicated by double or single quotes) and strings are returned from String calls in the context of a non-constructor (that is, without using a new keyword) - these are primitive strings . JavaScript automatically converts primitives and String objects so that you can use String Methods for primitive strings. In the context where the method is to be called in a primitive string or property search, JavaScript will automatically wrap the string primitive and call the method or perform a property search.
All of this is explained in detail in the specification , but it is not quite a simple read. I recently asked a question (about why this can be done above), so it might be worth reading a (very) detailed answer.
source share