[sf-perl] OffTopic: Any JavaScript programmers here?

Kevin Frost biztos at mac.com
Fri Jul 4 20:31:24 PDT 2008


Noooo! Don't poison the novices with innerHTML!  That's like telling  
people not to 'use strict' in Perl.

But the general point about deep copying is correct, by default JS  
uses what we would call "references."


  -- frosty

(via iPhone)


On Jul 4, 2008, at 7:25 PM, yary <not.com at gmail.com> wrote:

> Keeping this on-list since it's also a general coding question. I
> think the issue is shallow vs deep copying an object. When you have a
> line like
>
> var theList = document.getElementById(ListHandle);
>
> then theList is a handle into the list as a whole, and if the elements
> change order in the document, then they change in theList as well.
> What you need is called a "deep copy", where you walk through the
> object's elements/attributes, and copy all those as well.
>
> In general you need to think about how to copy each element as well-
> do you want to deep copy recursively all the way down? In this
> example, we might, if you're sorting the sublists as well. And we can
> get a deep copy by freezing a string copy-
>
>  var origList = theList.innerHTML; // a way in perl: use Storable,
> $origList = freeze \@theList;
>
> to set it back
>
>  theList.innerHTML = origList; // in perl, @theList = @{ thaw  
> $origList };
>
>
> I assume you're still working on this page, as the "sort these" links
> are currently out of order- I'll recommend using firebug in ff while
> debugging, though there are other page insepection/js debug tools and
> if you have one you like stick with it.
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


More information about the SanFrancisco-pm mailing list