From 38adb5e0fa65e51fccbd7d4e4428689d14164d7b Mon Sep 17 00:00:00 2001 From: SergioCrisostomo Date: Fri, 6 Feb 2015 01:41:53 +0100 Subject: [PATCH] after review improvements --- Source/Core/Core.js | 54 +++++++++++++++++++++++++++----------------------- Source/Types/Object.js | 10 +++++++--- Specs/Core/Core.js | 2 -- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Source/Core/Core.js b/Source/Core/Core.js index 7dda5182..d4521d4b 100644 --- a/Source/Core/Core.js +++ b/Source/Core/Core.js @@ -63,14 +63,19 @@ for (var i in {toString: 1}) enumerables = null; if (enumerables) enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor']; /**/ var hasOwnProperty = Object.prototype.hasOwnProperty; -function eachKey(object, fn, thisArg){ - for (var k in object) fn.call(thisArg, k, object[k]); +function objectKeys(object, alsoPrototypeChain){ + var keys = []; + for (var k in object){ + if (alsoPrototypeChain) keys.push(k); + else hasOwnProperty.call(object, k) && keys.push(k); + } /**/ if (enumerables) for (var i = enumerables.length; i--;){ k = enumerables[i]; - if (hasOwnProperty.call(object, k)) fn.call(thisArg, k, object[k]); + if (hasOwnProperty.call(object, k)) keys.push(k); } /**/ + return keys; } // Function overloading @@ -81,8 +86,12 @@ Function.prototype.overloadSetter = function(usePlural){ var self = this; return function(a, b){ if (a == null) return this; - if (usePlural || typeof a != 'string') eachKey(a, self, this); - else self.call(this, a, b); + if (usePlural || typeof a != 'string'){ + var keys = objectKeys(a, true), k; + for (var i = 0; k = keys[i]; i++) self.call(this, k, a[k]); + } else { + self.call(this, a, b); + } return this; }; }; @@ -308,26 +317,6 @@ Number.extend('random', function(min, max){ // forEach, each, keys -Object.extend({ - - keys: function(object){ - var keys = []; - eachKey(object, function(key, value){ - if (hasOwnProperty.call(object, key)) keys.push(key); - }, this); - return keys; - }, - - forEach: function(object, fn, bind){ - Object.keys(object).forEach(function(key){ - fn.call(bind, object[key], key, object); - }); - } - -}); - -Object.each = Object.forEach; - Array.implement({ /**/ @@ -345,6 +334,21 @@ Array.implement({ }); +Object.extend({ + + keys: objectKeys, + + forEach: function(object, fn, bind){ + Object.keys(object).forEach(function(key){ + fn.call(bind, object[key], key, object); + }); + } + +}); + +Object.each = Object.forEach; + + // Array & Object cloning, Object merging and appending var cloneOf = function(item){ diff --git a/Source/Types/Object.js b/Source/Types/Object.js index c41223dc..dc88f6de 100644 --- a/Source/Types/Object.js +++ b/Source/Types/Object.js @@ -68,9 +68,13 @@ Object.extend({ }, values: function(object){ - return Object.keys(object).map(function(key){ - return object[key]; - }); + var values = []; + var keys = Object.keys(object); + for (var i = 0; i < keys.length; i++){ + var k = keys[i]; + values.push(object[k]); + } + return values; }, getLength: function(object){ diff --git a/Specs/Core/Core.js b/Specs/Core/Core.js index a185cbcf..686c1c30 100644 --- a/Specs/Core/Core.js +++ b/Specs/Core/Core.js @@ -811,7 +811,6 @@ describe('Object.each', function(){ expect(daysObj).toEqual({first: 'Sunday', second: 'Monday', third: 'Tuesday'}); }); - /**/ it('should call non-enumerable properties too', function(){ var obj = { foo: 'bar', @@ -835,7 +834,6 @@ describe('Object.each', function(){ expect(keysInObject).toBeTruthy(); expect(iteration).toEqual(8); }); - /**/ }); -- 2.11.4.GIT