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?

Rodrigo Pereira
  • 1,834
  • 2
  • 17
  • 35
  • 1
    Check this http://stackoverflow.com/questions/17256182/javascript-string-literal-vs-string-object – PSL Nov 12 '13 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. – Johannes H. Nov 12 '13 at 22:29
  • 1
    @JohannesH. Not quite... [§4.3.2 primitive value](http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.2) – Jonathan Lonowski Nov 12 '13 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 ^^ – Johannes H. Nov 12 '13 at 22:31
  • @JohannesH. Yeah. "*Literals*" just refers to the syntax involved ([§7.8](http://www.ecma-international.org/ecma-262/5.1/#sec-7.8)), which most do create primitive values at runtime. – Jonathan Lonowski Nov 12 '13 at 22:33

3 Answers3

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
maček
  • 76,434
  • 37
  • 167
  • 198
  • 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. – basilikum Nov 12 '13 at 23:34
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.

Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
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.

angabriel
  • 4,979
  • 2
  • 35
  • 37