I was using objects as custom data structures for class initialization (
meaning var myWidget = new Widget( { foo: 'bar' } ) ), and I needed to be
able to build up a master configuration that was the combination of all of
the different sources.  The idea is that I would use configuration
'fragments' that were pre-defined in order to specify desired behavior.

So far, sounds like I should use $.extend(), right?

Problem is, I wanted it to merge them.  So, if there is a property of the
object that is itself an object, I want the properties of the copy to be
merged in with ( as a deep copy ) the target.  For example: $.extend( { foo:
{ a: 'a' } }, { foo: { b:'b' } } ) should yield { foo: { a:'a',b:'b' } }.

Still sounds like $.extend, just with the optional deep copy function turned
on, right?  $.extend(true,...)

But what if one of the properties is an array?  If I have an array of, for
example, event listeners already defined, and the incoming object defines
ANOTHER array, what should it do?  Naturally, it should merge the two
arrays, making one larger array, right?

And that's where $.extend falls down for me.

So, because $.extend couldn't quite do what I wanted, I knew I had to write
my own.  Which I did.  And I also created a bunch of test cases which define
my idea of what the object-merge function should do.  My function passes all
of those tests.  Yeah!  So, I then pointed those tests at
$.extend(true,...).  It passes 10 out of 25 tests.  Hmmm...

So, I'm recommending that we have a discussion about $.extend(true,...) and
whether or not it's failure to meet my expectations is a bug, or an
unreasonable expectation.  To that end, I have attached my qunit test file,
and my custom merge() function definition.  I would really appreciate it if
people would look over the test cases that I created, and weigh in on
whether or not the expected behavior is a reasonable thing to expect out of
$.extend(true,...), and if not, why not?

Anyone have any thoughts?

-- 
John Arrowwood
John (at) Irie (dash) Inc (dot) com
John (at) Arrowwood Photography (dot) com
John (at) Hanlons Razor (dot) com
--
http://www.irie-inc.com/
http://arrowwood.blogspot.com/
Marie von 
Ebner-Eschenbach<http://www.brainyquote.com/quotes/authors/m/marie_von_ebnereschenbac.html>
- "Even a stopped clock is right twice a day."

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en.


Attachment: test.extend.js
Description: JavaScript source

Attachment: merge.js
Description: JavaScript source

Reply via email to