PDA

View Full Version : [OPEN] Ext.clone() silently returns a reference to the object that you want to clone



mwrf
16 Feb 2012, 12:29 PM
Simple but serious bug.
Ext.clone() silently returns a reference to the passed object that you want
to clone, not a new object or undefined. This happens with objects that don't have
a function constructor.

For example, Cloning a model instance will cause this behavior:




Ext.define('Test', { extend: 'Ext.data.Model',
fields: [{name: 'age', type: 'int'}]
});


// instance of 'Test' model
var obj1 = Ext.create('Test', {
age : 1
});

// clone of obj1 - returns fine.
var obj2 = Ext.clone(obj1);

// set obj2 age to 99
obj2.data.age=99

// wait... what's this? obj2 was never a clone, just a pointer to obj1
console.log(obj1.data.age) //99




The offending line in Ext.clone() is this:

return clone || item;

Ext.clone() should return undefined if it can't do the clone,
NOT return a reference to the passed object when clone is undefined.

Present in 4.0.7 and 4.1.0-pr1 at least

mitchellsimoens
16 Feb 2012, 12:32 PM
I would agree but will let the core devs decide.

pillai
14 Jun 2013, 1:57 PM
is there any workaround there in ExtJs?

saga56
21 Nov 2017, 10:01 AM
4 years later and this is a bug? or a features? Because I really need to clone a object and not a reference :/