New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add instanceClone
parameter to lang/clone
#2
Conversation
Defines the behaviour of cloning instances of custom types
I think that if no func is passed, it should assume a custom one that does what underscore does which is create a new prototype and copy all the properties of the object as well as the prototype ones. This would fill @millermedeiros requirement in following underscore behaviour by default. |
OK, updated it to copy all properties on the prototype also. Would it be good to also have a function to clone an object, keeping the prototype? Maybe call it |
@conradz yes I think it would be useful to have a behaviour that would work for most instances. Maybe expose it in the clone itself and not as a new module? clone.cloneInstance = cloneInstance;
return clone; then clone(myInstance, clone.cloneInstance); @millermedeiros please give your feedback about all of this |
I don't think we need As I said on the issue 1 lodash and underscore behavior differs (lodash works like mout). It seems that underscore followed prototype.js behavior (copies properties from the prototype as well) while we use
A generic function instanceClone(instance){
return typeof instance.clone === 'function'? instance.clone() : createObject(instance);
} But this would cause issues with jQuery since their We should also consider adding a The truth is, the only times I needed methods like |
uhm... just put more thought on it and I think custom types should not be cloned. There is no way to clone it automatically on a reliable way. Maybe we keep the 2nd argument in case user want to handle it but by default it should just return the instance itself without cloning. // this is the behavior that I expect, custom types are not cloned, just passed by reference
// we only clone what we know we can clone safely
var a = { f: new Foo() };
var b = clone(a);
b.f === a.f; // true |
@millermedeiros I think that would probably be the best way. I think it would be better to keep it simple, but allow the user to define the behavior if they need it. |
I stripped the second commit, and defined the default behavior better now. It will now not attempt copying at all of instances created with a custom constructor, unless the |
function Custom() { } | ||
function cloneCustom(x) { return new Custom(); } | ||
var f = { test: new Custom() }; | ||
var g = clone(Custom, cloneCustom); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo, it should be var g = clone(f, cloneCustom);
@millermedeiros Good catches, I fixed both of them now. @satazor What do you think of this now? Would you agree with the way this works now? |
Yes I agree. clone and deepClone sounds also good to me. |
Specify that it will copy the object reference by default only if the type is non-native.
feel free to merge, makes way more sense than current behavior. PS: I will create a |
Add `instanceClone` parameter to `lang/clone`
See #1.
This adds an
instanceClone
parameter tolang/clone
, which allows passing a function that will be called to clone any non-native instance. A non-native instance is an object where.constructor
is notObject
.Opening this for more feedback. Is this the way it should behave?