Skip to main content

Bug Tracker

Side navigation

#2472 closed enhancement (worksforme)

Opened March 07, 2008 06:12AM UTC

Closed June 20, 2010 05:50PM UTC

$.clone

Reported by: frink Owned by:
Priority: minor Milestone:
Component: core Version:
Keywords: Cc:
Blocked by: Blocking:
Description

In writing bigger plugins it's often necessary to use clones inside of closures. JavaScript does everything by reference. but sometimes we need to create a clone. While the returned object have this capability: $().clone(); we can't use that in our plugins if what he need to clone a variable without trying to wade through the various ways to do this.

var frink = $.clone(frank);

code:

$.clone = function(obj) {

if(obj == null || typeof(obj) != 'object')

return obj;

var r = obj.split? [] : {};

for(var x in obj)

r[x] = clone(obj[x]);

return r;

}

Attachments (0)
Change History (2)

Changed August 15, 2008 10:55AM UTC by genezys comment:1

You could use $.extend() for objects.

var frink = $.extend({}, frank);

For arrays, concat() is great to make a fast copy:

var copy = array.concat();

Changed June 20, 2010 05:50PM UTC by dmethvin comment:2

resolution: → worksforme
status: newclosed

Between $.extend() and Array.concat.apply() it should be possible to do this already.