From 74c005c67862f6b4c86ad293c9262d711e148436 Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Wed, 14 Nov 2007 11:56:15 +0000 Subject: [PATCH] prototype: Remove the now redundant forked declaration of Hash#_each. --- CHANGELOG | 2 ++ src/hash.js | 39 +++++++++------------------------------ test/unit/hash.html | 11 +++++++++++ 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 66ab8ea..3dcdb36 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Remove the forked declaration of Hash#_each. As we are now systematically cloning the object upon instantiation, preventing iteration of shadowed properties is no longer required. [Tobie Langel] + * Performance optimizations for Event#findElement. Make Event#findElement's expression argument optional, in which case the extended target element is returned (same as Event#element). [Tobie Langel] * Ensure Event#fire always returns an extended event. [Tobie Langel] diff --git a/src/hash.js b/src/hash.js index dc0e905..b2d4872 100644 --- a/src/hash.js +++ b/src/hash.js @@ -3,35 +3,7 @@ function $H(object) { }; var Hash = Class.create(Enumerable, (function() { - if (function() { - var i = 0, Test = function(value) { this.key = value }; - Test.prototype.key = 'foo'; - for (var property in new Test('bar')) i++; - return i > 1; - }()) { - function each(iterator) { - var cache = []; - for (var key in this._object) { - var value = this._object[key]; - if (cache.include(key)) continue; - cache.push(key); - var pair = [key, value]; - pair.key = key; - pair.value = value; - iterator(pair); - } - } - } else { - function each(iterator) { - for (var key in this._object) { - var value = this._object[key], pair = [key, value]; - pair.key = key; - pair.value = value; - iterator(pair); - } - } - } - + function toQueryPair(key, value) { if (Object.isUndefined(value)) return key; return key + '=' + encodeURIComponent(String.interpret(value)); @@ -42,7 +14,14 @@ var Hash = Class.create(Enumerable, (function() { this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); }, - _each: each, + _each: function(iterator) { + for (var key in this._object) { + var value = this._object[key], pair = [key, value]; + pair.key = key; + pair.value = value; + iterator(pair); + } + }, set: function(key, value) { return this._object[key] = value; diff --git a/test/unit/hash.html b/test/unit/hash.html index ec214e8..cbb5f17 100644 --- a/test/unit/hash.html +++ b/test/unit/hash.html @@ -213,6 +213,17 @@ assertEqual("hello world", template.evaluate(hash.toObject())); assertEqual("hello world", template.evaluate(hash)); assertEqual("hello", "#{a}".interpolate(hash)); + }}, + + testPreventIterationOverShadowedProperties: function() { with(this) { + // redundant now that object is systematically cloned. + var FooMaker = function(value) { + this.key = value; + }; + FooMaker.prototype.key = 'foo'; + var foo = new FooMaker('bar'); + assertEqual("key=bar", new Hash(foo).toQueryString()); + assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString()); }} }, 'testlog');