在标准JavaScript ES6环境中,何时调用.toString()? [英] Within the standard JavaScript ES6 environment, when is .toString() ever called?

查看:74
本文介绍了在标准JavaScript ES6环境中,何时调用.toString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只发现调用.toString()的时间是通过字符串串联和字符串插值进行的:

I only found that the time .toString() is called is with string concatenation and string interpolation:

// Inside of Node:

> let arr = [1,3,5];

> arr.toString()
'1,3,5'

> "" + arr
'1,3,5'

> arr + ""
'1,3,5'

> `${arr}`
'1,3,5'

据说,console.log打印出对象的字符串表示形式并应该使用toString(),但是我不确定通常不是正确地实现toString(),所以console.log会做其他事情:

Supposedly, console.log prints out the string representation of the object and is supposed to use toString(), but I am not sure whether it is toString() not properly implemented usually, so console.log does something else:

> console.log(arr);
[ 1, 3, 5 ]

> console.log("arr is %s", arr);
arr is [ 1, 3, 5 ]

因此在JavaScript本身中,何时调用toString()?

So inside the JavaScript itself, when is toString() ever called?

我认为通过多态性,我们自己编写的任何内容,都可以使用ourObj.toString()来将对象的字符串表示形式表示为字符串.但是我想知道在JavaScript本身(所有功能,库,类)中,什么时候真正调用toString()?

I think by polymorphism, anything we write ourselves, we can use ourObj.toString() to get the string representation of our object as string. But I wonder within JavaScript itself (all its function, libraries, classes), when is toString() actually invoked?

推荐答案

在EcmaScript语言规范的多个部分中,提到了toString.一个重要的用途发生在抽象操作 OrdinaryToPrimitive :此函数将查找对象的toStringvalueOf方法,并执行它.优先级可能受 hint 参数的影响.

In several sections of the EcmaScript language specification, there is mention of toString. One important use occurs in the abstract operation OrdinaryToPrimitive: this function will look for the object's toString or valueOf method, and execute it. The precedence can be influenced by a hint argument.

依次,OrdinaryToPrimitive由抽象操作

In turn, OrdinaryToPrimitive is called by the abstract operation ToPrimitive

ToPrimitiveToNumberToStringToPropertyKey,关系比较,相等比较,表达式求值,Date构造函数,几种字符串化方法(如toJSON,...等等)调用

ToPrimitive is called by ToNumber, ToString, ToPropertyKey, relational comparison, equality comparison, evaluation of expressions, the Date constructor, several stringification methods, like toJSON, ...etc.

实际上,该语言充满了执行ToPrimitive的内部操作.该规范有200多个引用ToString.

In fact, the language is soaked with internal operations that will get to executing ToPrimitive. The specification has 200+ references to ToString.

这里是一个具有toString方法实现的对象,目的是证明toString在内部被调用.

Here is a object with a toString method implementation in order to prove that toString is called internally.

然后遵循几个表达式,每个表达式触发toString.

Then follow a few expressions that each trigger toString.

// Preparation
let obj = {
    toString() {
        this.i = (this.i||0) + 1; // counter
        console.log("call #" + this.i);
        return "0";
    }
};

// Trigger toString via several constructs
({})[obj];
1 < obj; 
1 == obj;
1 + obj;
1 - obj;
+obj;
Math.abs(obj);
parseInt(obj);
new Date(obj);
new RegExp(obj);
new Number(obj);
Symbol(obj); 
"".localeCompare(obj);
[obj, null].sort();
[obj].join();
`${obj}`;
setTimeout(obj); // Not standard EcmaScript environment, but defined in the agent.

如果在obj上定义了一个valueOf方法,该方法将返回原始值,则其中一些不会触发toString().

Some of these would not trigger toString() if there were a valueOf method defined on obj which would return a primitive value.

这篇关于在标准JavaScript ES6环境中,何时调用.toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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