为什么在字符串连接操作中不调用Number.toString()? [英] Why isn't Number.toString() being called on a string concatenation operation?

查看:85
本文介绍了为什么在字符串连接操作中不调用Number.toString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用 Object.toString()函数的默认行为时,我注意到下面的字符串连接可预测地调用 toString( )目标对象:

  var x = {toString :()=> 一个}; 
var y = {toString :()=> 两个};
var output ='x is'+ x +',而y是'+ y;
console.log(输出); //向控制台写x是一个而y是两个

然而,同样不是在 Number Boolean toString()时观察到例如,$ c>。有必要强制 toString()调用以获得所需的输出:

  Number.prototype.toString =()=> 42; 
Boolean.prototype.toString =()=> 无论你想要什么;

var a = 1;
console.log('答案是'+ a); //写道答案是1
console.log('答案是'+ a.toString()); //写道答案是42

var b = true;
console.log('女孩,你知道它'\\'''+ b); //写道女孩你知道这是真的
console.log('女孩你知道它'''''b.toString()); //写道女孩你知道它是你想要的

这在浏览器中是一致的(在Chrome上测试过) ,Firefox和Edge)所以我认为它是标准行为。它在哪里记录?是否存在在字符串连接期间获得特殊处理的标准对象列表?

解决方案

JavaScript将在数字原语和数字之间自由转换对象。



如果你看一下 + 运算符的规则,你会看到它说:


7让lprim为ToPrimitive(lval)。



9让rprim为ToPrimitive(rval)。


因此,在处理 + 时,它会尝试使用对象上的基元。



然后它有 ToString规则将原语转换为字符串。


Number见7.1.12.1。


...然后描述一个很长的特例






简而言之:



它将值转换为基元,然后具有将数字转换为字符串的特殊情况规则。



这意味着它不会将其视为Object或调用通常可以覆盖此类规则的 .toString 方法。 / p>

While experimenting with the default behavior of the Object.toString() function, I noticed that string concatenations like the one below predictably call toString() on the target objects:

var x = { toString: () => "one" };
var y = { toString: () => "two" };
var output = 'x is ' + x + ' while y is ' + y;
console.log(output); // writes "x is one while y is two" to the console

However, the same is not observed when toString() is overridden in the prototypes of Number and Boolean, for instance. It's necessary to "force" a toString() call in order to get the desired output:

Number.prototype.toString = () => "42";
Boolean.prototype.toString = () => "whatever you wish";

var a = 1;
console.log('The answer is ' + a); // writes "The answer is 1"
console.log('The answer is ' + a.toString()); // writes "The answer is 42"

var b = true;
console.log('Girl you know it\'s ' + b); // writes "Girl you know it's true"
console.log('Girl you know it\'s ' + b.toString()); // writes "Girl you know it's whatever you wish"

This is consistent across browsers (tested on Chrome, Firefox and Edge) so I presume it's standard behavior. Where is it documented? Is there a list of the standard objects that get special treatment during string concatenations?

解决方案

JavaScript will freely convert between number primitives and Number Objects.

If you look at the rules for the + operator you will see that it says:

7 Let lprim be ToPrimitive(lval).

and

9 Let rprim be ToPrimitive(rval).

So when dealing with + it will try to work with primitives over objects.

It then has the ToString rule to convert the primitive to a string.

Number See 7.1.12.1.

… which then describes a long special case.


In short:

It converts the value to a primitive and then has special case rules for converting numbers to strings.

This means it doesn't treat it as an Object or call the .toString method that could normally override such rules.

这篇关于为什么在字符串连接操作中不调用Number.toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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