clear prototype hash

23 views
Skip to first unread message

chrysanthe m

unread,
Oct 20, 2010, 10:39:49 PM10/20/10
to prototype-s...@googlegroups.com
Hello
How do I clear a prototype hash?  I have finally tried and failed with below after trying hashBrown.length=0 and hashBrown.clear(). 
  hashBown.each(function(key) {
        hashBrown.unset(key);
      });

T.J. Crowder

unread,
Oct 21, 2010, 2:51:50 AM10/21/10
to Prototype & script.aculo.us
Hi,
I'm surprised the loop didn't work, it seems to:
http://jsbin.com/ejeju4/2

Or you could add your own `clear` function, but you'd want to be sure
to check with each Prototype dot release that the implementation
didn't need to be tweaked:

* * * *
// Add Hash#clear
// Note that this must be RECHECKED on every Prototype dot release
// to ensure that the internals haven't changed.
(function() {
function Hash_clear() {
this._object = {};
}

Hash.prototype.clear = Hash_clear;
})();
* * * *
http://jsbin.com/ibipe4

I would _not_ suggest just doing hashBrown._object = {}; inline in
your code. Define a method and check the implementation on every dot
release, so there's only one place to add it.

I _know_ I've seen discussion of adding a #clear method to hash, but I
don't recall the result. As it's not in 1.7, maybe someone thought it
wasn't a good idea, but it seems like an odd omission to me. I've done
a ticket in Lighthouse[1] offering to do a patch on git (for 1.7.1,
not 1.7.0) if people do want it.

[1] https://prototype.lighthouseapp.com/projects/8886-prototype/tickets/1158-add-hashclear

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

T.J. Crowder

unread,
Oct 21, 2010, 3:15:34 AM10/21/10
to Prototype & script.aculo.us
Hi,

> I'm surprised the loop didn't work, it seems to:
> http://jsbin.com/ejeju4/2

Sorry, that link was wrong. It should be:
http://jsbin.com/ejeju4

-- T.J.

On Oct 21, 7:51 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> On Oct 21, 3:39 am, chrysanthe m <chrysant...@gmail.com> wrote:
>
> > Hello
> > How do I clear a prototype hash?  I have finally tried and failed with below
> > after trying hashBrown.length=0 and hashBrown.clear().
> >   hashBown.each(function(key) {
> >         hashBrown.unset(key);
> >       });
>
> I'm surprised the loop didn't work, it seems to:http://jsbin.com/ejeju4/2
>
> Or you could add your own `clear` function, but you'd want to be sure
> to check with each Prototype dot release that the implementation
> didn't need to be tweaked:
>
> * * * *
> // Add Hash#clear
> // Note that this must be RECHECKED on every Prototype dot release
> // to ensure that the internals haven't changed.
> (function() {
>   function Hash_clear() {
>     this._object = {};
>   }
>
>   Hash.prototype.clear = Hash_clear;})();
>
> * * * *http://jsbin.com/ibipe4
>
> I would _not_ suggest just doing hashBrown._object = {}; inline in
> your code. Define a method and check the implementation on every dot
> release, so there's only one place to add it.
>
> I _know_ I've seen discussion of adding a #clear method to hash, but I
> don't recall the result. As it's not in 1.7, maybe someone thought it
> wasn't a good idea, but it seems like an odd omission to me. I've done
> a ticket in Lighthouse[1] offering to do a patch on git (for 1.7.1,
> not 1.7.0) if people do want it.
>
> [1]https://prototype.lighthouseapp.com/projects/8886-prototype/tickets/1...

chrysanthe m

unread,
Oct 21, 2010, 12:34:20 PM10/21/10
to prototype-s...@googlegroups.com
Hi TJ
Thanks but any explanation why that closure didnt work?  Makes me wonder how to code any of those really elegant uses.  I relinquished to the irratating crude hashBrown=new Hash().  It just so disgusts me, grin.

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.


T.J. Crowder

unread,
Oct 21, 2010, 5:56:15 PM10/21/10
to Prototype & script.aculo.us
Hi,

> Thanks but any explanation why that closure didnt work?

Sorry, I just looked at your code again, and realized that I misread
it the first time (which is why my http://jsbin.com/ejeju4 worked but
yours didn't). You're doing

hashBrown.each(function(key) { ...

But that loops through the key/value *pairs*, not the keys, so the
`unset` call doesn't have any effect because there's no matching key.
You want:

hashBrown.keys().each(function(key) { ...

...as in the link above. (Your quoted code also had a typo --
"hashBown" rather than "hashBrown" -- but I assume that that's just a
typo in this message, or you'd be seeing an exception in your
debugger.)

With these things, sometimes the issues become obvious when you step
through the code with a debugger. Might have helped here, if you did
that and saw the values for `key`...

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

> > prototype-scripta...@googlegroups.com<prototype-scriptaculou s%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
Message has been deleted
0 new messages