BTW: you are missing a final ")"
Haha, ok it's official - I suck. Or at least am not allowed to paste
code in here that I haven't at least given a quick firebug trial ; ).
1) I tried to replace my version with it and it didn't work. I did add
in the $.clone(xxx,true) to my code. It wasn't obvious why it didn't
work.
Can you paste the code you tried and that shows the function failing?
2) Speed. Cloning can be quite slow due to the recursion. Is it
possible to move the jQuery specific stuff to the end so it's not
always run by default ?
Give me a break ; ). I would of course need to benchmark this, but I
doubt you'd gain any significant speed by that. Optimizing your jQuery
selectors is a *much* better place to worry about speed. That being
said, I think none of the stuff could be moved to the bottom as
$.isFunction could theoretically return true for objects that have the
constructor Object (somebody correct me if I'm wrong).
-- Felix
--------------------------
My Blog: http://www.thinkingphp.org
My Business: http://www.fg-webdesign.de
weepy wrote:
BTW: you are missing a final ")"
2 comments :
1) I tried to replace my version with it and it didn't work. I did add
in the $.clone(xxx,true) to my code. It wasn't obvious why it didn't
work.
2) Speed. Cloning can be quite slow due to the recursion. Is it
possible to move the jQuery specific stuff to the end so it's not
always run by default ?
On Aug 5, 6:46 pm, the_undefined <[EMAIL PROTECTED]> wrote:
Sry for spamming the list, but I just spotted another error (that's
what you get when doing quick refactorings for posting stuff in public
without testing them properly ^^). Full code again / last post:
$.extend({
clone: function(obj, deep) {
// Clone a jQuery object / objects with a custom clone function
if (obj && obj && obj.clone && $.isFunction(obj.clone)) {
return obj.clone(deep);
}
// Clone a function
if ($.isFunction(obj)) {
return function() {return obj.apply(this, arguments);};
}
if (obj && obj.constructor == Array) {
// Clone an array
var clone = [];
for(var i = 0; i < obj.length; i++) {
clone[i] = (deep == true)
? obj[i]
: $.clone(obj[i], deep);
}
return clone;
} else if (obj && obj.constructor == Object) {
// Clone an object
var clone = {};
for (var p in obj) {
clone[p] = (deep == true)
? obj[p]
: $.clone(obj[p], deep);
}
return clone;
}
return obj;
}
}
-- Felix
On Aug 5, 7:42 pm, the_undefined <[EMAIL PROTECTED]> wrote:
Ups, that last line should read:
return obj;
not 'return clone;'.
On Aug 5, 7:30 pm, Felix Geisendörfer <[EMAIL PROTECTED]> wrote:
My function for cloning looks like this: It covers objects, arrays,
functions and jQuery objects / custom objects with an own clone() function:
------------------------------------------------------------------------------------------------------------------------------------------------------
$.extend({
clone: function(obj, deep) {
// Clone a jQuery object / objects with a custom clone function
if (obj && obj && obj.clone && $.isFunction(obj.clone)) {
return obj.clone(deep);
}
// Clone a function
if ($.isFunction(obj)) {
return function() {return obj.apply(this, arguments);};
}
if (obj && obj.constructor == Array) {
// Clone an array
var clone = [];
for(var i = 0; i < obj.length; i++) {
clone[i] = (deep == true)
? obj[i]
: $.clone(obj[i], deep);
}
return clone;
} else if (obj && obj.constructor == Object) {
// Clone an object
var clone = {};
for (var p in obj) {
clone[p] = (deep == true)
? obj[p]
: $.clone(obj[p], deep);
}
}
return clone;
}
------------------------------------------------------------------------------------------------------------------------------------------------------
I'm interested to hear feedback,
-- Felix
--------------------------
My Blog:http://www.thinkingphp.org
My Business:http://www.fg-webdesign.de
Erik Beeson wrote:
Thanks for sharing this. I'm pretty sure what you suggest won't
properly deal with arrays. Objects aren't the same as arrays. Here's
how I do something like that:
function deepCopy(obj) {
if(obj && obj.constructor == Object) {
var newObj = new Object();
for(var field in obj) {
newObj[field] = deepCopy(obj[field]);
}
return newObj;
} else if(obj && obj.constructor == Array) {
var newArray = new Array();
for(var i = 0; i < obj.length; i++) {
newArray[i] = deepCopy(obj[i]);
}
return newArray;
}
return obj;
}
Also, while it's fine to use $ however you want in your own code, for
code that you're making public, I suggest you checkout the plugin
authoring guide about not using $ directly:
http://docs.jquery.com/Plugins/Authoring#Custom_Alias
--Erik
On 8/5/07, *weepy * <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
wrote:
During assingment, if the object is not primative, Javascript will
return a pointer to the object rather than a copy.
E.g.
a = [1,2]
b = a
b[0]=3
a ==> [3,2]
This clone function makes it possible to copy an object.
$.clone = function (obj) {
if(typeof(obj) != 'object') return obj;
if(obj == null) return obj;
var newobj = new Object();
for(var i in obj)
newobj[i] = $.clone(obj[i]);
return newobj;
}
a = [1,2]
b = $.clone(a)
b[0]=3
a ==> [1,2]
I have found this function invaluable when comparing and complex
Javascript objects.