What is the "===" operator for?
I once encountered an operator "===". But I don remember what it was.. or where do we use it .. or is t开发者_JAVA技巧here any such a kind of operator ? where is it used ??
In PHP, JavaScript, ECMAScript, ActionScript 3.0, and a number of other similar, dynamic languages, there are two types of equality checks: == (non-strict equality) and === (strict equality). To show an example:
5 == "5" // yep, these are equal, because "5" becomes 5 when converted to int
5 === "5" // nope, these have a different type
Basically, whenever you use ==, you risk automatic type conversions. Using === ensures that the values are logically equal AND the types of the objects are also equal.
In JavaScript, ==
does type coercion, while ===
, the "strict equality" operator does not. For example:
"1" == 1; // true
"1" === 1; // false
There is also a corresponding strict inequality operator, !==
.
Its used in JavaScript, PHP and may be more (which I may not have encountered yet!), it is used to compare if both the compared things are of same object type as well as have same value.
"===" operator is used to check the values are equal as well as same type.
Example
$a === $b if $a is equal to $b, and they are of the same type.
It usually tests if two objects are the same. ie. not if they have the same value(s) but if they really are the same object.
=== is equality, at least in PHP
Here is a link that helps explain thsi
In Ruby, triple equals is the operator (implicitly) used by the case/when
construct to determine when an object "falls within" a particular case. For example, Ruby has a concept of "Range" objects; 1..10
means "all of the values between 1 and 10, inclusive." So `3 == 1..10' is false, since the 3 is a number and the 1..10 is a Range. But,
3 === 1..10
returns true, since 3 is in that range.
case/when uses this when deciding which case an arguments belongs to. So,
case a
when (1..10)
puts "This is a valid rating"
else
puts "invalid"
end
works as expected.
精彩评论