Defined is useful for when an object can be coerced to false but is defined. For example: function test(item) { if (item) alert('item was defined'); else alert('item was not defined'); }
test(false); //alerts that the item was not defined, though it is; it's just defined as false function betterTest(item) { if ($defined(item)) alert('item was defined'); else alert('item was not defined'); } The key here is that item is passed in; so it is named, even if it is undefined. You can't reference Drag on its own in the window scope because, if it's not there, it isn't named. But you *can* reference a property of window, even if it isn't defined, which is why "if (window.Drag)..." works. So long as you aren't testing for values that might be defined but evaluate as false (empty strings, zero, the boolean false) then you don't really need to use $defined. On Tue, Apr 28, 2009 at 2:29 PM, jonlb (via Nabble) < ml-user+57835-2049679...@n2.nabble.com<ml-user%2b57835-2049679...@n2.nabble.com> > wrote: > > So could I do $defined(window.Drag) and get the result I wanted? If > that's so, then what's the better way to do this? if I can test just > by doing > > if (window.Drag) {} > > then what's the point of $defined? > > I think I'm missing something here...... > > Thanks for all the help, > jonlb > > On Apr 28, 2:00 pm, nutron > <anut...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737060&i=0>> > wrote: > > > if (window.Drag) {... drag exists...} > > if (window.Drag && Drag.Move) { ... Drag.Move exists ... } > > > > On Tue, Apr 28, 2009 at 1:57 PM, jonlb (via Nabble) < > > ml-user+57835-2049679...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737060&i=1> > <ml-user%2b57835-2049679...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737060&i=2>> > > > > > > > > > > wrote: > > > > > So, just to be sure I understand.... If I want to be able to check > > > for the presence of the Drag mootools-more class (say to see if it was > > > included or not for optional functionality), I can't just do $defined > > > (Drag). I would need to use the typeof operator instead. > > > > > So by extension, that would go for any class (even my own custom > > > ones). Correct? > > > > > Thanks, > > > jonlb > > > > > On Apr 28, 1:44 pm, Fábio Costa <fabiomco...@...< > http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2736868&i=0>> > > > wrote: > > > > > > anyFunction(undefinedValue) will always throw you an error. You can't > > > > pass a > > > > non defined param to a function. > > > > > > For this to work you should declare the var before using the > function, in > > > > > > this case: > > > > > > var undefinedValue; > > > > anyFunction(undefinedValue); > > > > > > Will work. > > > > > > In you case: > > > > > > var Drag; > > > > $defined(Drag); > > > > > > Will return false. > > > > > > Fábio Miranda Costa > > > > Engenheiro de Computaçãohttp://meiocodigo.com > > > > > > On Tue, Apr 28, 2009 at 5:26 PM, jonlb <jo...@...< > http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2736868&i=1>> > > > wrote: > > > > > > > Hello All, > > > > > > > I was wondering why it is that if I try to check if a class is > defined > > > > > by doing, for instance, $defined(Drag) it will through an error if > > > > > Drag wasn't available. I would expect a false rather than an error. > > > > > > > > Am I missing something or does this seem to be counter intuitive? > > > > > > > Thanks, > > > > > jonlb > > > > > ------------------------------ > > > View message @ > > >http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p273686... > > > > To start a new topic under MooTools Users, email > > > ml-node+660466-1583815...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737060&i=3> > <ml-node%2b660466-1583815...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737060&i=4>> > > > > To unsubscribe from MooTools Users, click here< (link removed) >. > > > > ----- > > The MooTools Tutorial: http://www.mootorial.comwww.mootorial.com > > Clientcide: http://www.clientcide.comwww.clientcide.com > > -- > > View this message in context: > http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p273688... > > Sent from the MooTools Users mailing list archive at Nabble.com. > > > ------------------------------ > View message @ > http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p2737060.html > To start a new topic under MooTools Users, email > ml-node+660466-1583815...@n2.nabble.com<ml-node%2b660466-1583815...@n2.nabble.com> > To unsubscribe from MooTools Users, click here< (link removed) >. > > > ----- The MooTools Tutorial: http://www.mootorial.com www.mootorial.com Clientcide: http://www.clientcide.com www.clientcide.com -- View this message in context: http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p2737101.html Sent from the MooTools Users mailing list archive at Nabble.com.