admin管理员组

文章数量:1205165

In Javascript, the == comparison has a strict (non-type converting) version: ===. Likewise, != has the strict form !==. These protect you from the following craziness:

var s1 = "1",
    i1 = 1,
    i2 = 2;

(s1 == i1)   // true, type conversion
(s1 != i1)   // false, type conversion

(s1 === i1)  // false, no type conversion
(s1 !== i1)  // true, no type conversion

However, the other comparison operators have no equivalent strict modes:

(s1 < i2)   // true, type conversion
(s1 <= i2)  // true, type conversion
([] < i2)   // true, wait ... wat!?

The obvious solution seems pretty verbose:

((typeof s1 === typeof i2) && (s1 < i2))  // false

Is there a more idiomatic (or just less verbose) way to do this in Javascript?

Reference: MDN Comparison Operators

In Javascript, the == comparison has a strict (non-type converting) version: ===. Likewise, != has the strict form !==. These protect you from the following craziness:

var s1 = "1",
    i1 = 1,
    i2 = 2;

(s1 == i1)   // true, type conversion
(s1 != i1)   // false, type conversion

(s1 === i1)  // false, no type conversion
(s1 !== i1)  // true, no type conversion

However, the other comparison operators have no equivalent strict modes:

(s1 < i2)   // true, type conversion
(s1 <= i2)  // true, type conversion
([] < i2)   // true, wait ... wat!?

The obvious solution seems pretty verbose:

((typeof s1 === typeof i2) && (s1 < i2))  // false

Is there a more idiomatic (or just less verbose) way to do this in Javascript?

Reference: MDN Comparison Operators

Share Improve this question edited Oct 26, 2012 at 17:03 kanaka asked Oct 26, 2012 at 16:46 kanakakanaka 73.1k23 gold badges147 silver badges143 bronze badges 12
  • 1 But don't those operators only make sense if both operands are of the same type? What would you expect for something like [] < 1, or {} >= []? – bfavaretto Commented Oct 26, 2012 at 16:49
  • @bfavaretto But "2" >== 1 should return false – Ruan Mendes Commented Oct 26, 2012 at 16:50
  • I think he is saying something more along the lines of "1" < 2. I use strict cases to prevent stupid logic errors in my code, and I assume that is what he is doing as well. – thatidiotguy Commented Oct 26, 2012 at 16:51
  • ((s1 === i2) && (s1 < i2)) is not working? – Adam Stelmaszczyk Commented Oct 26, 2012 at 16:52
  • 2 You might be able to build your own with a preprocessor/macros for JavaScript such as sweet.js – clentfort Commented Oct 26, 2012 at 16:59
 |  Show 7 more comments

2 Answers 2

Reset to default 14

There are no built-in operators for what you want, but you can always create your own functions. For example, for <:

function lt(o1, o2) {
    return ((typeof o1 === typeof o2) && (o1 < o2));
}
lt("10", 11); // false

Another option, if you're only dealing with strings and numbers, is extending String.prototype and Number.prototype:

function lt(o) {
    return ((typeof this.valueOf() === typeof o) && (this < o));
}
String.prototype.lt = lt;
Number.prototype.lt = lt;
"10".lt(11);   // false
(11).lt("12"); // false

How about creating a Object and using it

var strictComparison = {
    "<" : function(a,b) { return ((typeof a === typeof b) && (a < b)) },
    "<=" : function(a,b) { return ((typeof a === typeof b) && (a <= b)) },
    ">" : function(a,b) { return ((typeof a === typeof b) && (a > b)) },
    ">=" : function(a,b) { return ((typeof a === typeof b) && (a >= b)) }
};

console.log(strictComparison["<"](5,"6")) ;  
console.log(strictComparison[">"](5,6)) ;   

本文标签: