PDA

View Full Version : minor YAHOO.ext.CompositeElementLite code weirdness



gcsolaroli
10 Nov 2006, 2:01 AM
Digging on the ComponentElement.js file, I found some other code that didn't look perfectly sound


YAHOO.ext.CompositeElementLite = function(els){
this.elements = [];
this.addElements(els);
this.el = YAHOO.ext.Element.get(this.elements[0], true);
};


There is nothing wrong with this constructor, but I think it would be more correct to write it like this, wouldn't it?

YAHOO.ext.CompositeElementLite = function(els){
YAHOO.ext.CompositeElementLite.superclass.constructor.call(this, els);
this.el = YAHOO.ext.Element.get(this.elements[0], true);
};



There is also a subtle inconsistency in the YAHOO.ext.CompositeElementLite.addElements implementation.

YAHOO.ext.CompositeElementLite
addElements : function(els){
if(!els) return this;
this.elements = this.elements.concat(els);
},


This code does not return anything when some elements are passed, whereas in all other condition (even in the YAHOO.ext.CompositeElement.addElement) 'this' is always returned.

This could be easily fixed:

YAHOO.ext.CompositeElementLite
addElements : function(els){
if (els) {
this.elements.concat(els);
}
return this
},

jack.slocum
10 Nov 2006, 2:48 AM
There used to be a reason the parent's constructor was not called. That code was removed and the code in Lite was never changed.

I went through and added return this; as an after thought. I must have missed a spot. Thanks.

FYI, this code wouldn't work, concat does not modify the original array.

addElements : function(els){
if (els) {
this.elements.concat(els);
}
return this
}

gcsolaroli
10 Nov 2006, 3:00 AM
FYI, this code wouldn't work, concat does not modify the original array.

Thanks for you correction.

I just quickly looked for an online example on how to use concat on an array, as I was not sure if it was modifying the original or not, but I have misunderstood how to use it.

jack.slocum
10 Nov 2006, 3:02 AM
No, thank you for pointing these things out. Right now is the time to correct the weirdness. :)