How to print the properties and methods of javascript String object.
Following snippet does not print anything.
for (x in String) {
document.write(x);
}
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"...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With