From 3898597b12fc80a70e5585d5a99c66ce8b52b6d8 Mon Sep 17 00:00:00 2001 From: Arlo Breault Date: Wed, 18 Jan 2012 20:34:54 -0800 Subject: [PATCH] has --- underscore.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/underscore.js b/underscore.js index 9c8bcc2b0..b6671d50b 100644 --- a/underscore.js +++ b/underscore.js @@ -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. @@ -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; } } @@ -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)); }; }; @@ -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; }; @@ -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; } @@ -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; }; @@ -788,7 +787,7 @@ }; if (!_.isArguments(arguments)) { _.isArguments = function(obj) { - return !!(obj && hasOwnProperty.call(obj, 'callee')); + return !!(obj && _.has(obj, 'callee')); }; } @@ -838,6 +837,11 @@ return obj === void 0; }; + // Has own property? + _.has = function(obj, key) { + return ObjProto.hasOwnProperty.call(obj, key); + }; + // Utility Functions // -----------------