Is it possible to improve this type with the "object"?

if (typeof a !== "object" && typeof b !== "object") {
    return a == b;
}
... // check pairwise equality of object a & b using `for in`

This is the same as

if (typeof a !== "object") {
    return a == b;
}

Is there ba typeof b === "object", which will change the semantics?

Are there any terrible cases that I should know about? Comparison between objectand a native type, which have non-intuitive logical equality or poor quality? Including any errors in the browser (I mean IE6 you!)

+3
source share
3 answers

The second check is not quite the same as the first, no, simply because JavaScript is weakly typed, so at least consider the effect of " .toString()", as well as others. For example, this may not perform the first check, but pass the second:

var a = "[object Object]";
var b = {};

, ( , ... ):

var a = 0;
var b = "0";

=== , ... , , " ".

+1

.
, , :

var a = "a string";
var b = new String("a string");
console.log(typeof a);//"string" !== "object"
console.log(typeof b);//"object" === "object"
console.log('"' + a + '" ' + (a==b?"==":"!=") + ' "' + b + '"');

:

var a = 1;
var b = new Number(1);

if .
"" instanceof :

if ((typeof a !== "object" || a instanceof Number || a instanceof String) &&
    (typeof b !== "object" || b instanceof Number || b instanceof String))
    return a == b;
+1

isEqual Underscore.js. " , , ". . :

  // Perform a deep comparison to check if two objects are equal.
  _.isEqual = function(a, b) {
    // Check object identity.
    if (a === b) return true;
    // Different types?
    var atype = typeof(a), btype = typeof(b);
    if (atype != btype) return false;
    // Basic equality test (watch out for coercions).
    if (a == b) return true;
    // One is falsy and the other truthy.
    if ((!a && b) || (a && !b)) return false;
    // Unwrap any wrapped objects.
    if (a._chain) a = a._wrapped;
    if (b._chain) b = b._wrapped;
    // One of them implements an isEqual()?
    if (a.isEqual) return a.isEqual(b);
    // Check dates' integer values.
    if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
    // Both are NaN?
    if (_.isNaN(a) && _.isNaN(b)) return false;
    // Compare regular expressions.
    if (_.isRegExp(a) && _.isRegExp(b))
      return a.source     === b.source &&
             a.global     === b.global &&
             a.ignoreCase === b.ignoreCase &&
             a.multiline  === b.multiline;
    // If a is not an object by this point, we can't handle it.
    if (atype !== 'object') return false;
    // Check for different array lengths before comparing contents.
    if (a.length && (a.length !== b.length)) return false;
    // Nothing else worked, deep compare the contents.
    var aKeys = _.keys(a), bKeys = _.keys(b);
    // Different object sizes?
    if (aKeys.length != bKeys.length) return false;
    // Recursive comparison of contents.
    for (var key in a) if (!(key in b) || !_.isEqual(a[key], b[key])) return false;
    return true;
  };

See the Underscore.js source code to see the rest of the functions used by this.

It’s easy to overlook some extreme cases, so I would recommend using well-tested code like this instead of reinventing the wheel.

+1
source

Source: https://habr.com/ru/post/1792913/


All Articles