Why does console.dir ('') print "no properties"?

I got the impression that strings have properties like match . Why console.dir('') claim that '' has no properties (at least in Google Chrome)?

+6
source share
3 answers

This is because '' is a string literal and not an instance of the String class. Since properties like match are declared on String.prototype , you will not see them when using the string literal. If you use the new operator, you will see what you expected :

 var s = new String("hello"); console.dir(s); 

Here is a screenshot from the Chrome developer tools (note the need to extend the prototype , because the method you expect to see is declared on the prototype, not on the String object):

enter image description here

+6
source

Probably for the same reason that console.dir (true) and console.dir (1234) say that after you release the handle that points to the data. The code probably only iterates through the properties if it is an object. Why this rotate button is still unclear.

+2
source

In JavaScript, the global String object has methods predefined in the language. Actual string literals inherit the methods of the global String object, but otherwise have no properties other than "length".

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String

0
source

Source: https://habr.com/ru/post/904122/


All Articles