[英]'hello' == (anything that will return true other than 'hello') in JavaScript?
I understand that ==
in JavaScript is comparison with type coercion. 我知道JavaScript中的
==
是与强制类型的比较。 And I know that the following statements are true
: 我知道以下陈述是
true
:
'' == false;
' ' == false;
'0' == false;
'\n' == false;
However, I can't get a comparison with 'hello' on the left side to be true: 但是,我无法与左侧的“ hello”作比较:
'hello' == true; // no this is false
'hello' == false; // no this is false
'hello' == 1; // no this is false
'hello' == 0; // no this is false
Is there anything 'hello' can be compared to which results in true
other than 'hello'? 有什么“你好”可以被比作一个结果
true
不是“你好”等?
There is this one: 有一个:
if('hello') {
alert('true')
}
This will be evaluated as true
because the string isn't empty or null
. 因为字符串不为空或
null
所以将其评估为true
。
Since you want a compare: 由于您想要比较:
'hello' == String.fromCharCode.apply(String, [104, 101, 108, 108, 111])
这算吗?
["hello"] == "hello" // true
Here's one: 这是一个:
var x = ['H', 'e', 'l', 'l', 'o'];
x.toString = function() {
return this.join("");
}
alert(x == "Hello"); // true
http://jsfiddle.net/jfriend00/KSgwb/ http://jsfiddle.net/jfriend00/KSgwb/
Or another: 或其他:
var x = {
toString: function() {return "Hello";}
}
alert(x == "Hello"); // true
http://jsfiddle.net/jfriend00/hKx9x/ http://jsfiddle.net/jfriend00/hKx9x/
If you study the coercion rules for ==
, you will find that the only thing that will satisfy ==
with "Hello"
is something that already is a string that is "Hello"
or something that has a .toString()
method that returns "Hello"
. 如果研究
==
的强制性规则,您会发现,唯一满足“ ==
"Hello"
是已经是"Hello"
的字符串或具有.toString()
方法的返回值"Hello"
。
That can be done in as many creative ways as you want by joining an array, returning the string directly, processing a bunch of character codes that combine to form that string, etc... But, in the end, .toString()
has to return "Hello"
in order to satisfy the ==
test. 可以通过加入数组,直接返回字符串,处理一堆组合在一起形成该字符串的字符代码等多种方式以所需的多种方式来实现。但是,最后,
.toString()
具有返回"Hello"
以满足==
测试。
If you aren't allowing the thing you're comparing to to have "Hello"
in it in any way or be able to produce that string upon demand, then NO there is nothing else that will satisfy ==
except something that produces the string "Hello"
when asked to coerce to a string. 如果您不允许所比较的事物以任何方式包含
"Hello"
或无法按需生成该字符串,那么没有其他满足==
除非会生成该字符串当要求您强制使用字符串时, "Hello"
。
Here's a layman's description of the type coercion rules for Javascript: http://webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html 这是外行对Java语言的类型强制规则的描述: http : //webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html
In a nutshell, here are the coercion rules when a string is involved: 简而言之,这是涉及字符串时的强制规则:
If both types are a string, then the comparison is true
only if the two strings contain exactly the same characters. 如果两种类型都是字符串,则仅当两个字符串包含完全相同的字符时,比较才为
true
。
If one is a string and the other is a number, then try to convert the string to a number and compare that to the other number. 如果一个是字符串,另一个是数字,则尝试将字符串转换为数字,然后将其与另一个数字进行比较。 Since
Number("Hello")
is NaN
, this will never work for a number since NaN can't be ==
to another number. 由于
Number("Hello")
是NaN
,因此这对于数字将永远无效,因为NaN不能==
另一个数字。
If one is a string and the other is an object, call the internal method valueOf
if it's defined or toString
if it's not defined and compare the result of that to your string. 如果一个是字符串,另一个是对象,则调用内部方法
valueOf
如果已定义)或toString
如果未定义),然后将其结果与您的字符串进行比较。
If one is a string and the other is a Boolean, convert both to a number and compare them. 如果一个是字符串,另一个是布尔值,则将两者都转换为数字并进行比较。 Since
Number("Hello")
is NaN
, it will never match a Boolean which will either be 0
or 1
when converted to a Number. 由于
Number("Hello")
为NaN
,因此永远不会与转换为Number时为0
或1
的布尔值匹配。 For example: true == "1"
. 例如:
true == "1"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.