Typeof, Instanceof, and Constructor
While working on a project, I came across a confusing failing test:
createElement(node) does, in fact, return a string. I went to the console to experiment.
These things seemed to be in contradiction. How could
n be a type
of string, where the constructor is the String function,
but is not in fact an instance of String?
With the help of James Porter, I found
this helpful StackOverflow post
So, for example, creating a string literal
is different from creating a string object using the
These two vars, while they both behave as strings as far as we are concerned, are not the same:
one is a literal and the other is an instance.
The syntax for
object instanceof constructor
where it "tests whether an object has in its prototype chain the prototype property of a constructor" (from the MDN docs). And since literals are not an object, while a string created by the String constructor is, the string literal is not an instance of String.
05 Oct 2016