Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print the javascript string object properties and methods

Tags:

javascript

How to print the properties and methods of javascript String object.

Following snippet does not print anything.

for (x in String) {
   document.write(x);   
}
like image 815
minil Avatar asked Dec 20 '22 07:12

minil


1 Answers

The properties of String are all non-enumerable, which is why your loop does not show them. You can see the own properties in an ES5 environment with the Object.getOwnPropertyNames function:

Object.getOwnPropertyNames(String);
// ["length", "name", "arguments", "caller", "prototype", "fromCharCode"]

You can verify that they are non-enumerable with the Object.getOwnPropertyDescriptor function:

Object.getOwnPropertyDescriptor(String, "fromCharCode");
// Object {value: function, writable: true, enumerable: false, configurable: true}

If you want to see String instance methods you will need to look at String.prototype. Note that these properties are also non-enumerable:

Object.getOwnPropertyNames(String.prototype);
// ["length", "constructor", "valueOf", "toString", "charAt"...
like image 142
James Allardice Avatar answered Feb 19 '23 17:02

James Allardice