Confusion with Object.prototype.toString and primitive string datatype
On Jan 1, 2016, at 10:56 PM, Bob Myers <rtm at gol.com> wrote:
Seems like a good Stack Overflow question, take it there.
Don’t we have any mailing list to ask JavaScript questions? I like mailing list rather than StackOverflow.com :(
This mailing list is largely meant for the further development of JavaScript as a language (new feature design, spec review, etc.), and not for questions on how JavaScript works. StackOverflow is indeed a better venue for such questions.
And to answer, the 'k'
in Object.prototype.toString.call( 'k' )
get
auto-boxed. That's why.
Because javascript is dynamically typed, conversion from objects to
primitive values and vice versa. This is done respectively by the
internal [[ToPrimitive]]
and [[ToObject]]
methods. See
Object.prototype.toString
, step 3
Let O be ToObject(this value).
people.mozilla.org/~jorendorff/es6-draft.html#sec-toobject
ToObject
, creates a String
object whose value is that of the passed-in string.
Hello All,
Although as you said this list is not meant for question/answers, you explained it. Thanks for the explanation. This is my first and last question in this list as per the list standards. I would still say if there is another list for question/answers the community will be much more helpful than earlier.
Again thanks to everyone.
Consider the below simple code :
'k' instanceof String; // false new String('k') instanceof String; // true
Now, When I am inspecting them as below why in both cases the output comes as [object String] ?
The below is understood as per the result of the
instanceof
operator.Object.prototype.toString.call( new String('k') ); // "[object String]”
My confusion comes when I see the below code output :
Object.prototype.toString.call( 'k' ); // "[object String]”
Can anyone please explain it ?