Wednesday, 15 April 2015

javascript - Why does Chrome console.log display String objects in such an annoying way? -


The primary string appears well in the Chrome console. But string objects come in such a way:

  s = new string ('foo bar') ==> String {0: "F", 1: "O", 2: "O", 3: "", 4: "B", 5: "A", 6: "R", Length: 7, [[PrimitiveValue] ]]: "Foo bar"}  

I know that I have it with s + '' or s.toString () . But I often need to see the arrays and other structures filled with these things, so I get a little less assistant:

  [s, s, s] ==> [String, String, String]  

And when I expand one of the strings, then I get a vertical representation of the entire character array.

If there is no good answer, someone might at least express gratitude with me.

The absurd answer is that chrome shows it in this way because it's the string object to understand For that chrome, try to put an old thing in ancient times?

The comments of Dandvise are correct on the money - depending on your usage, you can modify the string prototype, then use the antiquity for readability and depend on it (thus your methods are available ):

Javascript automatically converts primitives into string objects, so it is possible to use string object methods for ancient strings.

  String.prototype.upper = function () return to {this.toUpperCase (; }; Document.write ("foo bar" .upper ());  

If you really wanted, you could write the same object

 

function mystring (value) {this ['[[primitive value]]'] = value; (var i = 0; i & lt; value for this.length = value.length; .length; i ++) {this [i] = value.charAt (i);} this.charAt = function (val) {return it [val];}; this.toString = function () {this return.primitive }; This return;} var myString = new MyString ('foo bar') ; Var string = New string ('FU bar'); Document.write ('Custom:', 'Mystring,' 'Original:', string, '
Check also the console'); Console.log 'Custom:', myString, 'native:', string)

Whatever will output to the console like this 1: "O "F.U. Bar", Length: 7, Charite: Function, 2: "O", 4: "B", 5: "A", 6: "R", [[Primitive]]: Toasting: function}


No comments:

Post a Comment