Return Value of Function as items Pattern
2. April 2008 – 17:37,items:(function(){ var items = []; // populate items array return items; }).createDelegate(this)()
For good of all productive developers
,items:(function(){ var items = []; // populate items array return items; }).createDelegate(this)()
4 Responses to “Return Value of Function as items Pattern”
A good example on how to use this pattern:
That code will render a combobox for selecting one year between 1900 and the current year, basically an “year selector”.
By Lucian on May 26, 2008
@Lucian
Exactly! I see that you grasped it fully. You can use same pattern also for items array if you need to.
By Saki on May 26, 2008
I don\’t understand what the difference is with
items:function()
{
var items[];
.
.
.
return items;
}()
By zeruyo on Aug 26, 2009
the difference is scope : in case you need to refer to properties/methods of the class itself (`this`) inside the function.
also, i think it\’s a good practice to NOT use inline functions (closures) this way, but to refer to defined methods of the class, or to \’helper\’ methods.
correct me if i\’m wrong .
By thezver on Mar 3, 2010