jQuery extend nested object -
when try extend object, passing in full object, seems overwrite desired target object. however, can extend each subobject individually, , pass in , works.
$(function() { window.test = {}; $.extend(window.test, { init: function(opts1, opts2) { //method 1, overwrites this.options = $.extend({}, this.settings, {opts1: opts1, opts2: opts2}); //method 2, works this.a = $.extend({}, this.settings.opts1, opts1); this.b = $.extend({}, this.settings.opts2, opts2); this.options2 = $.extend({}, this.settings, {opts1: this.a, opts2: this.b}); console.log(this.options, this.options2); }, settings: { opts1: { a: 'hello', b: 'hi', e: 'this still here?' }, opts2: { c: 'yo', d: 'wassup' } } }); test.init({ a: 'hello2', b: 'hello3' }); });
my question if there way utilize first method (a 1 line, simple method) achieve results of second method (individual subobjects).
also, here's jsfiddle.
you can deep-copy extends passing true
first parameter $.extend
. believe asking here.
this.options = $.extend(true, this.settings, {opts1: opts1, opts2: opts2});
see second definition in documentation.
Comments
Post a Comment