print the javascript string object properties and methods - Stack Overflow

How to print the properties and methods of javascript String object.Following snippet does not print an

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

Following snippet does not print anything.

for (x in String) {
   document.write(x);   
}

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

Following snippet does not print anything.

for (x in String) {
   document.write(x);   
}
Share Improve this question asked Apr 10, 2013 at 7:36 minilminil 7,15516 gold badges51 silver badges57 bronze badges 1
  • possible duplicate of Is it possible to get the non-enumerable inherited property names of an object? – Joseph Commented Apr 10, 2013 at 7:46
Add a ment  | 

4 Answers 4

Reset to default 9

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"...

First It must be declared as Object,(may be using 'new' keyword)

s1 = "2 + 2";               
s2 = new String("2 + 2");   
console.log(eval(s1));      
console.log(eval(s2));      

OR

console.log(eval(s2.valueOf()));

try using the console in developer tools in Chrome or Firebug in Firefox.

and try this

    for (x in new String()) {
       console.log(x);   
    }

This should do the job :

 var StringProp=Object.getOwnPropertyNames(String);

 document.write(StringProp); 

 -->>  ["length", "name", "arguments", "caller", "prototype", "fromCharCode"]

But you might be more interested by :

 var StringProtProp=Object.getOwnPropertyNames(String.prototype);

 document.write(StringProtProp); 

-->> ["length", "constructor", "valueOf", "toString", "charAt", "charCodeAt", "concat", 
"indexOf", "lastIndexOf", "localeCompare", "match", "replace", "search", "slice", "split", 
"substring", "substr", "toLowerCase", "toLocaleLowerCase", "toUpperCase", "toLocaleUpperCase",
 "trim", "trimLeft", "trimRight", "link", "anchor", "fontcolor", "fontsize", "big", "blink", 
"bold", "fixed", "italics", "small", "strike", "sub", "sup"]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744349011a4569864.html

相关推荐

  • print the javascript string object properties and methods - Stack Overflow

    How to print the properties and methods of javascript String object.Following snippet does not print an

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信