console.log 是否调用对象的 toString 方法? [英] Does console.log invokes toString method of an object?

查看:83
本文介绍了console.log 是否调用对象的 toString 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此文档

附加了这些对象中的每一个的字符串表示按照列出的顺序一起输出.

The string representations of each of these objects are appended together in the order listed and output.

也根据答案

+x 将对象 x 强制转换为字符串,也就是 [object对象]:

The + x coerces the object x into a string, which is just [object Object]:

所以,我的问题是

如果我这样做

str = new String("hello")
console.log(str) //prints the string object but not 'hello'
console.log(""+str) //prints "hello"

因此,在第一种情况下,它只是打印对象(不调用 toString() 方法).

So, in first case, it simply prints the object (doesn't invoke the toString() method).

但在第二种情况下,它不会强制而是简单地打印原始值.为什么会这样?

But in second case, it doesn't coerce but simply print the primitive value. Why is that so?

console.log 调用哪个方法来打印对象?

Which method does console.log invokes to print the object?

请注意 - 这不是此 问题.

Please note that - this is not a duplicate of this question.

推荐答案

Console API 不是在任何规范中定义的标准 API,而是在所有浏览器中实现的东西,因此供应商通常可以自由实现他们自己的风格,因为没有标准规范来定义 API 中任何方法的输出.

Console API is not a standard API that is defined in any specification but is something that is implemented across all browsers, so vendors are usually at their liberty to implement in their own fashion as there's no standard spec to define the output of any methods in API.

除非您检查特定浏览器的控制台 API 的实际实现,否则您永远无法确定.GitHub 上有一个跟踪器,列出了主要浏览器实现之间的差异.

Unless you check the actual implementation of the Console API for a particular browser, you can never be sure. There's a tracker on GitHub listing the differences between implementation from major browsers.

如果您查看 FF 中的实现(可用 here - 搜索日志),下面有评论

If you look at the implementation in FF (available here - search for log), it has a comment below

对象的多行字符串化,专供人类使用

A multi line stringification of an object, designed for use by humans

实际实现检查传递给 log() 的参数类型,并根据它的类型生成不同的表示.

The actual implementation checks for the type of argument that is passed to log() and based on it's type, it generates a different representation.

针对您的情况,log() 为使用 literal 表示法创建的字符串和使用 String 构造函数创建的字符串打印两个不同的值,因为它们是两种不同的类型.正如此处所述,使用创建的字符串文字符号称为String Primitives,使用String 构造函数创建的字符串称为String Objects.

Coming to your case, log() prints two different values for strings created using literal notation and strings created using String constructor because they are two different types. As explained here, Strings created using literal notation are called String Primitives and strings created using String constructor are called String Objects.

var str1 = 'test';
var str2 = new String('hello');

typeof str1 // prints "string"
typeof str2 // prints "object"

由于类型不同,它们的字符串表示在控制台 API 中有所不同.如果你仔细阅读FF的Console实现的代码,最后一句是

As the types differ, their string representation differs in the Console API. If you go through the code for FF's Console implementation, the last statement is

return "  " + aThing.toString() + "
";

因此,为了回答您的问题,仅当参数类型不是 {undefined,null,object,set,map} 之一时,FF 中的控制台 API 才会对参数调用 toString() 类型.它并不总是调用 toString()valueOf() 方法.我没有检查Chrome的实现,所以我不会对此发表评论.

So to answer your question, Console API in FF calls toString() on the argument only if the argument type is not one of {undefined,null,object,set,map} types. It doesn't always call toString() or valueOf() methods. I didn't check the implementation of Chrome, so I won't comment on that.

这篇关于console.log 是否调用对象的 toString 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆