Related Posts
- 14 Sep 2020 » Scone Heads
- 13 Sep 2020 » Pretty Good Therapy
- 03 Dec 2016 » Clock
While working on a project, I came across a confusing failing test:
Why, though? 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
which explains how string literals "are a different kind of object than objects created from within Javascript."
So, for example, creating a string literal
is different from creating a string object using the String
function.
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.
JavaScript is a prototype-based language (versus class-based). So, objects aren't instances of a class. Rather, you can only check if an object has another object in its prototype chain somewhere.
The syntax for instanceof
is
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.
JavaScript wat.
05 Oct 2016