From b31f642e646f7b35ad0fe966766f8ba8c922dd41 Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Fri, 23 May 2008 13:31:29 -0500 Subject: [PATCH] Ensure Hash does not return keys from the prototype chain (e.g., constructor, valueOf, toString). --- CHANGELOG | 2 ++ src/hash.js | 4 +++- test/unit/hash_test.js | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 67dc3c6..92f58bf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Ensure Hash does not return keys from the prototype chain (e.g., constructor, valueOf, toString). (kangax) + * Fix toString/valueOf sharing same method reference via closure in Class#addMethods. Use plain property assignment, since Object.extend fails to enumerate over toString/valueOf. (kangax) * Stop Form.Element.disable from stealing focus. (jddalton) diff --git a/src/hash.js b/src/hash.js index 125a96b..ed96637 100644 --- a/src/hash.js +++ b/src/hash.js @@ -28,7 +28,9 @@ var Hash = Class.create(Enumerable, (function() { }, get: function(key) { - return this._object[key]; + // simulating poorly supported hasOwnProperty + if (this._object[key] !== Object.prototype[key]) + return this._object[key]; }, unset: function(key) { diff --git a/test/unit/hash_test.js b/test/unit/hash_test.js index f67ebca..cc3d083 100644 --- a/test/unit/hash_test.js +++ b/test/unit/hash_test.js @@ -14,6 +14,9 @@ new Test.Unit.Runner({ this.assertEqual('A', h.get('a')); this.assertUndefined(h.a); this.assertUndefined($H({}).get('a')); + + this.assertUndefined($H({}).get('toString')); + this.assertUndefined($H({}).get('constructor')); }, testUnset: function() {