Skip to content

Commit

Permalink
Merge pull request #440 from arlolra/has
Browse files Browse the repository at this point in the history
has
  • Loading branch information
jashkenas committed Jan 23, 2012
2 parents f6a1b97 + 3898597 commit f1ecedf
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions underscore.js
Expand Up @@ -26,8 +26,7 @@
// Create quick reference variables for speed access to core prototypes.
var slice = ArrayProto.slice,
unshift = ArrayProto.unshift,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
toString = ObjProto.toString;

// All **ECMAScript 5** native function implementations that we hope to use
// are declared here.
Expand Down Expand Up @@ -80,7 +79,7 @@
}
} else {
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
if (_.has(obj, key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
Expand Down Expand Up @@ -506,7 +505,7 @@
hasher || (hasher = _.identity);
return function() {
var key = hasher.apply(this, arguments);
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};

Expand Down Expand Up @@ -612,7 +611,7 @@
_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
for (var key in obj) if (_.has(obj, key)) keys[keys.length] = key;
return keys;
};

Expand Down Expand Up @@ -733,17 +732,17 @@
if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
// Deep compare objects.
for (var key in a) {
if (hasOwnProperty.call(a, key)) {
if (_.has(a, key)) {
// Count the expected number of properties.
size++;
// Deep compare each member.
if (!(result = hasOwnProperty.call(b, key) && eq(a[key], b[key], stack))) break;
if (!(result = _.has(b, key) && eq(a[key], b[key], stack))) break;
}
}
// Ensure that both objects contain the same number of properties.
if (result) {
for (key in b) {
if (hasOwnProperty.call(b, key) && !(size--)) break;
if (_.has(b, key) && !(size--)) break;
}
result = !size;
}
Expand All @@ -762,7 +761,7 @@
// An "empty" object has no enumerable own-properties.
_.isEmpty = function(obj) {
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (hasOwnProperty.call(obj, key)) return false;
for (var key in obj) if (_.has(obj, key)) return false;
return true;
};

Expand All @@ -788,7 +787,7 @@
};
if (!_.isArguments(arguments)) {
_.isArguments = function(obj) {
return !!(obj && hasOwnProperty.call(obj, 'callee'));
return !!(obj && _.has(obj, 'callee'));
};
}

Expand Down Expand Up @@ -838,6 +837,11 @@
return obj === void 0;
};

// Has own property?
_.has = function(obj, key) {
return ObjProto.hasOwnProperty.call(obj, key);
};

// Utility Functions
// -----------------

Expand Down

0 comments on commit f1ecedf

Please sign in to comment.