PDA

View Full Version : [2.1 (rev 2042)] Broken pattern: this.id = this.id || Ext.id()



coderobo
15 Aug 2008, 4:06 PM
All over in ext-all-debug.js I see this pattern :


this.id = this.id || Ext.id();or


getId : function(){
return this.id || (this.id = "ext-comp-" + (++Ext.Component.AUTO_ID));
},
If I pass in a ctor config id as numeric 0, you can see the code
would break and end up using use Ext.id() instead of using the number 0 as the id.

Correct pattern to use would be -

this.id = this.id == undefined ? Ext.id() : this.id;

hendricd
15 Aug 2008, 4:47 PM
Correct pattern to use would be -

this.id = this.id == undefined ? Ext.id() : this.id;

Careful, think about what you've proposed there.
In Javascript undefined, null, false, and 0 are equivalent in the statement:
this.id = this.id || Ext.id();Truth table:


when: then id becomes: the problem:
this.id ==0 this.id = 0 Duplicate DOM/Component ID
this.id ==null this.id = null Invalid DOM/Component ID
this.id == undefined this.id = unique None