How to copy/clone a hash/object in JQuery?

Yes, extend an empty object with the original one; that way, everything will simply be copied:

var clone = $.extend({}, settings);

Extending some filled object with another, e.g.:

$.extend({a:1}, {b:2})

will return:

{a:1, b:2}

With the same logic:

$.extend({}, {foo:'bar', test:123})

will return:

{foo:'bar', test:123}

i.e. effectively a clone.

Leave a Comment