if we use angular.equal to compare to variables
it doing the following steps to match all the cases
function equals(o1, o2) {
if (o1 === o2) return true;
if (o1 === null || o2 === null) return false;
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2 && t1 == 'object') {
if (isArray(o1)) {
if (!isArray(o2)) return false;
if ((length = o1.length) == o2.length) {
for (key = 0; key < length; key++) {
if (!equals(o1[key], o2[key])) return false;
}
return true;
}
} else if (isDate(o1)) {
if (!isDate(o2)) return false;
return equals(o1.getTime(), o2.getTime());
} else if (isRegExp(o1)) {
if (!isRegExp(o2)) return false;
return o1.toString() == o2.toString();
} else {
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) ||
isArray(o2) || isDate(o2) || isRegExp(o2)) return false;
keySet = createMap();
for (key in o1) {
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
if (!equals(o1[key], o2[key])) return false;
keySet[key] = true;
}
for (key in o2) {
if (!(key in keySet) &&
key.charAt(0) !== '$' &&
isDefined(o2[key]) &&
!isFunction(o2[key])) return false;
}
return true;
}
}
to be
function equals(o1, o2) {
if (areBothEqualValue(o1, o2)) return true;
if (isAnyOneHasValueEqualNull(o1, o2)) return false;
if (areBothValuesEqualNaN(o1, o2)) return true;
if (areBothObjectsEqual(o1, o2)) return true;
return false;
}
I didn't change any logic or and all cases are checked in the same order as
before.
I refactored it by extracting functions with a meaningful names. to make it
tell the story to reader
now it clear that we are doing the following steps
1 - check if their values are equal using areBothEqualValue(o1, o2) function
2 - check if one of them is null using AnyOneHasValueEqualNull(o1, o2)
function
3 - check if their values equal NAN using areBothValuesEqualNaN(o1, o2)
function
4 - check if two objects are using equal areBothObjectsEqual(o1, o2)
function
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.