admin管理员组文章数量:1319470
When I create objects in Javascript, and log them out to the console, I nearly always see a Javascript object in the console. For example
var myObj = {
bla: "foo"
}
console.log(myObj);
will output as
{
bla: "foo"
}
However, Date
acts differently.
When I create a Date, it somehow knows to print out a "String" representing the date of the Date object.
For example:
var myObj = {
date: new Date()
}
console.log(myObj);
gets me
{
date: Sun Oct 25 2020 18:36:19 GMT-0700 (GMT-04:00)
}
What is the underlying mechanism here? How can I do the same with my own objects ?
When I create objects in Javascript, and log them out to the console, I nearly always see a Javascript object in the console. For example
var myObj = {
bla: "foo"
}
console.log(myObj);
will output as
{
bla: "foo"
}
However, Date
acts differently.
When I create a Date, it somehow knows to print out a "String" representing the date of the Date object.
For example:
var myObj = {
date: new Date()
}
console.log(myObj);
gets me
{
date: Sun Oct 25 2020 18:36:19 GMT-0700 (GMT-04:00)
}
What is the underlying mechanism here? How can I do the same with my own objects ?
Share Improve this question asked Oct 26, 2020 at 1:38 CodyBugsteinCodyBugstein 23.4k67 gold badges224 silver badges381 bronze badges 2-
hmn, looks like you still see a javascript object ;) and the string is the actual output of
new Date()
whenever it gets toString like when using$('input').val(new Date())
you get the same result without console – john Smith Commented Oct 26, 2020 at 1:45 -
1
There is no standard covering console output, so you see whatever the implementation developers want you to see. For Dates, the SO console appears to call
toISOString
, browser consoles appear to calltoString
, other environments may do other things. – RobG Commented Oct 26, 2020 at 5:54
3 Answers
Reset to default 5This is a special exception the browser has made for Date objects, to make them easy to examine when logged. Otherwise, if the default behavior was followed, you'd get an empty object with no own-properties and no useful information about what the object actually contains:
However, you can observe somewhat similar behavior and display an arbitrary string when something is logged if the thing that's logged gets coerced to a string, by putting a toString
method on the object:
const obj = {
toString() {
return 'foobar';
}
};
console.log(String(obj));
To get something that actually logs like Date objects do without any coercion on your part, you'd have to change the browser's internal code. It's not something doable from JS.
There are a few other sorts of built-in objects that have special logging behavior, including:
- Arrays
- Functions
- Errors
It means that Date object has toString
method implemented under the hood.
More reading: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
Actually, the new Date() constructor function does not print a string in the console. It looks like a string but run the typeof operator against it and you'll see it's an object:
const date = new Date()
console.log(date)
console.log(typeof date)
Also, if you are a React developer and will try to use the above date variable between curly brackets in order to output the dynamic content, you'll get an error. If it was a string it would work flawlessly. It is so because objects in the JSX expressions are invalid while strings are perfectly valid.
Again, the result of calling new Date() is an object. It does not look like one but it definitely is an object.
It is also possible to call Date() constructor function without the new keyword in front of it. And guess what? The result of such a call is a normal string that can be used inside of the JSX expression.
const date = Date()
console.log(date)
console.log(typeof date)
本文标签: How does Javascript Date print out a string when you console log itStack Overflow
版权声明:本文标题:How does Javascript Date print out a string when you console log it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742060245a2418539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论