admin管理员组文章数量:1279207
The documentation on mozilla states: "Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", ..."
Sounds logical: A toString() method will be called. So I defined an own "class" and defined a toString() method:
function DataField(name, dataType) {
// ....
}
DataField.prototype.toString = function() {
return this.type.name + " " + this.name;
}
I instantiate an object like this:
var dataField = new DataField("myint", DataType.INTEGER);
I try to output the object respectively the string representation of the object in NodeJS:
console.log(dataField);
console.log("" + dataField);
console.log(dataField.toString());
The first output differs from the other ones. The first one will give some kind of JSON.stringify() representation of the object. toString() is not called automatically as I'd expect it to be. The second and third output will result in exactly what I'd expect:
INTEGER myint
So I'm a bit confused. That means that toString() will not automatically be called though the explanation of mozilla suggests. Is this a regular behaviour in NodeJS or did I missinterpret the explanation on mozilla or am I missing something else here?
You might think: Well, then let's just call toString() explicitely if necessary and everything is fine. But there is some inconvenience with that. If I create an array of objects and use console.log() to ouput that array, no toString() will be called. Which means: The output generated is useless. This would mean that if I store objects with explicite implementations of toString() within an array in a container object I will have to implement toString() of the container object in a very specific and incovenient way: I must write code to explicitely create a string representations of any array of objects contained in the container object myself. Of course I could do that, but I would intuitively assume a toString() method to be called automatically therefor eliminating such need for explicite implementations.
Regarding NodeJS it seems that finding information is not that easy on this specific problem. So if you know about this topic any help is appreciated. The question is: What is the most elegant way to create a (human readable) string representation of a data model (= a data container object containing lists of other data objects)?
The documentation on mozilla states: "Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", ..."
Sounds logical: A toString() method will be called. So I defined an own "class" and defined a toString() method:
function DataField(name, dataType) {
// ....
}
DataField.prototype.toString = function() {
return this.type.name + " " + this.name;
}
I instantiate an object like this:
var dataField = new DataField("myint", DataType.INTEGER);
I try to output the object respectively the string representation of the object in NodeJS:
console.log(dataField);
console.log("" + dataField);
console.log(dataField.toString());
The first output differs from the other ones. The first one will give some kind of JSON.stringify() representation of the object. toString() is not called automatically as I'd expect it to be. The second and third output will result in exactly what I'd expect:
INTEGER myint
So I'm a bit confused. That means that toString() will not automatically be called though the explanation of mozilla suggests. Is this a regular behaviour in NodeJS or did I missinterpret the explanation on mozilla or am I missing something else here?
You might think: Well, then let's just call toString() explicitely if necessary and everything is fine. But there is some inconvenience with that. If I create an array of objects and use console.log() to ouput that array, no toString() will be called. Which means: The output generated is useless. This would mean that if I store objects with explicite implementations of toString() within an array in a container object I will have to implement toString() of the container object in a very specific and incovenient way: I must write code to explicitely create a string representations of any array of objects contained in the container object myself. Of course I could do that, but I would intuitively assume a toString() method to be called automatically therefor eliminating such need for explicite implementations.
Regarding NodeJS it seems that finding information is not that easy on this specific problem. So if you know about this topic any help is appreciated. The question is: What is the most elegant way to create a (human readable) string representation of a data model (= a data container object containing lists of other data objects)?
Share Improve this question asked Jan 18, 2016 at 23:25 Regis MayRegis May 3,4663 gold badges35 silver badges55 bronze badges 5- Is I'd stated above in some contexts in the JavaScript world a toString() will be called automatically on generation of output. And that behaviour is typical in other programming languages as well. – Regis May Commented Jan 18, 2016 at 23:40
-
In some contexts it would be called indeed, and in some it would not. It's not obvious how calling it for every object passed to
console.log
would be convenient to anyone.console.log({}.toString()) // [object Object]
I want to see the object contents, not a string. – zerkms Commented Jan 18, 2016 at 23:41 - In my use case it is petely the other way around: I'd prefer a short human readable representation, not the data itself in all it's detail. And exactly that's the strategy in other programming languages if someone implements a toString() method. Output is then automatically converted to a string using that method - which I wongly expected it to be the case using console.log(). Nevertheless thanks to all - everything has already been clarified. – Regis May Commented Jan 19, 2016 at 0:08
-
1
So your case is particular to you. And generic case is useful to anyone. One important thing:
console.log
IS NOT standardised, so you cannot rely on its behaviour anywhere. – zerkms Commented Jan 19, 2016 at 0:25 - This is the generic case in a variety of other programming languages. Nevertheless: It'd thought console.log() would be part of the standard. Thanks for the information that this is not the case. – Regis May Commented Jan 19, 2016 at 0:43
1 Answer
Reset to default 16In Node.js, console.log
calls util.format
, which does not call toString()
.
本文标签: nodejsJavaScript toString() seems not to be called automatically in NodeJSStack Overflow
版权声明:本文标题:node.js - JavaScript toString() seems not to be called automatically in NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741296689a2370859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论