JavaScript中的强制 [英] Coercion in JavaScript

查看:120
本文介绍了JavaScript中的强制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一些关于胁迫的事情。

I was wondering a few things about coercion.

当你这样做时:

1 == true // true

哪一个被强制进入哪一个?是左边还是右边?

Which one is coerced into which one ? is it the left one or the right one ?

当你这么做时

undefined == null // true

它是如何工作的?
以何种顺序尝试转换它?
实例:

How does it work exactly ? In which order does it try to convert it ? By instance:

1)    String(undefined) == String(null) // false
2)    Number(undefined) == Number(null) // false
3)    Boolean(undefined) == Boolean(null) // true

它是否首先尝试强制左侧操作数?那么对吧?然后两个?

Does it first try to coerce the left side operand ? then the right ? then both ?

编辑:
如评论中所述:
不重复。虽然这两个问题都是关于类型强制的,但这个问题哪个操作数被强制转换为另一个。另一个是关于评估强制类型的真实来源

As explained in the comments: "not a duplicate. While both questions are about type coercion, this one asks which operand get coerced into the other. The other one is about the source of truth when evaluating the coerced types"

推荐答案

这个过程描述于 7.2.12抽象平等比较


比较x == y,其中x和y是值,产生true或false。这样的比较如下进行:


  1. 如果类型(x)是相同的如Type(y),则返回执行Strict Equality Comparison x === y的结果。

  1. If Type(x) is the same as Type(y), then return the result of performing Strict Equality Comparison x === y.

如果x为null且y未定义,则返回true 。

If x is null and y is undefined, return true.

如果x未定义且y为null,则返回true。

If x is undefined and y is null, return true.

如果类型(x)是数字,类型(y)是字符串,返回比较结果x == ToNumber(y)。

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

如果类型(x) )是String,Type(y)是Number,返回比较结果ToNumber(x)== y。

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

如果Type(x)是布尔值,返回比较结果ToNumber(x)== y。

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

如果Type(y)是布尔值,则返回比较结果x == ToNumber(y)。

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

如果Type(x)是String,Number或Symbol而Type(y)是Object,则返回结果比较x == ToPrimitive(y)。

If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).

如果Type(x)是Object和Type(y)i s字符串,数字或符号,然后返回比较结果ToPrimitive(x)== y。

If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.

返回false。


因此,不是强迫一方,而是强迫另一方,或类似的东西,更多的是解释器通过上面的列表直到它找到匹配的条件,并执行生成的命令,这可能只涉及强制左侧,或者只有右侧(并且很少,两者都是,如果达到递归命令,例如使用 true =='1',这将满足条件8,转入 1 =='1',履行条件6并转入 1 == 1 ,满足条件3并解析为 true

So rather than coercing one side and then the other, or something like that, it's more that the interpreter goes through that list above until it finds a matching condition, and executes the resulting command, which may involve coercing only the left side, or only the right side (and, rarely, both, in case a recursive command is reached, such as with true == '1', which will fulfill condition 8, turn into 1 == '1', fulfilling condition 6 and turning into 1 == 1, fulfilling condition 3 and resolving to true)

这篇关于JavaScript中的强制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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