6

I am currently learning javascript and there is something that I don't understand.

//This means that I am using a method from the String.prototype
"ThisIsMyString".length

So, if I use ("ThisIsMyString" instanceof String) was suppose to return true, wasn't it? But turns out that returns false.. and I belive that is because the primitive type.

Here is my question: If "ThisIsMyString" is not an instance of String, how it can access property from that Object? What's the part of the puzzle that I don't know?

5
  • 1
    Check this stackoverflow.com/questions/17256182/… Commented Nov 12, 2013 at 22:22
  • Be careful with terms: Javascript has no "primitive types", but it has "literals" (as most languages). They are not considered separate types however but language constructs that don't really fit the OO model. Commented Nov 12, 2013 at 22:29
  • 1
    @JohannesH. Not quite... §4.3.2 primitive value Commented Nov 12, 2013 at 22:30
  • @JonathanLonowski: oh. ok, well then ;) I have to admit, I never read the spec, but most references only use the term literals, that'S why I was under the impression that it is the only term used here. Doesn't mka too much difference though ^^ Commented Nov 12, 2013 at 22:31
  • @JohannesH. Yeah. "Literals" just refers to the syntax involved (§7.8), which most do create primitive values at runtime. Commented Nov 12, 2013 at 22:33

3 Answers 3

1

String.length is not

a method from the String.prototype

length is a property of String

See the MDN docs for String.length


To answer your question though, the reason "hello" instanceof String returns false lies within how instanceof actualy works

Object.getPrototypeOf("hello")
// TypeError: Object.getPrototypeOf called on non-object

However, this is how your string literal has access to these methods/properties

"my string".constructor === String
// true

"my string".__proto__ === String.prototype
// true

If you want an actual instance of String

var str = new String("hello");
str instanceof String
// true
Sign up to request clarification or add additional context in comments.

1 Comment

The facts that "my string".constructor === String and "my string".__proto__ === String.prototype are not the answer, but part of the same behaviour. The reason is the dot operator, which converts a string literal temporarily into a String object to perform the desired operation.
0

You are able to use such methods because the language recognizes and converts the primitive type to a temporary instance of the String object and returns the length property of that object.

2 Comments

Just in case you noticed: Downvoted your initialy response, as it just wasn't true. Better now ;)
@JohannesH. haha no worries! You were more than right to down vote me before I fixed my silliness.
0

Taken from http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html: "JavaScript implicitly wraps the string primitive in a String object, processes the String object property or method call, and then discards the object"

Read further for details.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.