I was thinking about this pattern/problem some more and while I like the
window.has pattern I think if your going to adopt it, that particular
interface is too limiting for the use case. One place where I have used this
pattern is checking my JSON response objects especially from 3rd parties.


Usage:

$exists('Fx.Morph'); // defaults to window scope, looks for Fx, if found
looks for Morph within Fx.

$exists(['Drag','Move']); // properties can be passed in as an array of
strings rather then dot notation.

$exists('feed/entry',json_resp); // alternative path notation, applied to a
json response obj.


Code:

function $exists(prop,scope) {
    var parts = ($type(prop) == 'array') ? prop : prop.split(/\.|\//);
    var obj = scope || window;
    for (var i = 0; i < parts.length; i++) {
        obj = obj[parts[i]];
        if (obj == undefined) return false;
    }
    return true;
}






On Tue, Apr 28, 2009 at 4:58 PM, nwhite <changereal...@gmail.com> wrote:

>
> sure. Again, I don't see the need for such a thing as I don't use this
>> pattern so often...
>>
>
> Agreed. I generally think its a bad pattern however it could be useful in
> more complex scenarios. It might be an interesting mechanism for sanity
> checking with lazy loading code.
>
>
>
>>
>>
>> On Tue, Apr 28, 2009 at 4:08 PM, nwhite (via Nabble) <
>> ml-user%2b93763-1341009...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737607&i=0>
>> > wrote:
>>
>>>
>>>> window.has('foo.bar') or something...
>>>>
>>>
>>> ah I like that.
>>>
>>> Window.implement({
>>>  has : function(prop){
>>> var parts = prop.split('.'), obj = window;
>>>  for(var i = 0; i < parts.length; i++){
>>> obj = obj[parts[i]];
>>> if(obj == undefined) return false;
>>>  }
>>> return true;
>>> }
>>>  });
>>>
>>>
>>>
>>>
>>>
>>>>
>>>>
>>>> On Tue, Apr 28, 2009 at 2:55 PM, nwhite (via Nabble) <
>>>> ml-user%2b93763-1341009...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737486&i=0>
>>>> > wrote:
>>>>
>>>>> is there any issue with the $classExists function above?
>>>>>
>>>>> it seems to me it would be a little less verbose in some cases and
>>>>> clearer to understand. for example if I want to test for Request.JSONP
>>>>>
>>>>> if(!!window.Request && !!window.Request.JSONP ) // do something
>>>>>
>>>>> instead becomes:
>>>>>
>>>>> if($classExists('Request.JSONP') // do something
>>>>>
>>>>> On Tue, Apr 28, 2009 at 2:47 PM, nutron 
>>>>> <anut...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737207&i=0>
>>>>> > wrote:
>>>>>
>>>>>> there's no point in doing this inside a conditional. ! and !! perform
>>>>>> coercion, as does the conditional.
>>>>>>
>>>>>>
>>>>>> On Tue, Apr 28, 2009 at 2:38 PM, Sanford Whiteman [Mobile] (via
>>>>>> Nabble) 
>>>>>> <ml-user%2b64022-1734430...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2737159&i=0>
>>>>>> > wrote:
>>>>>>
>>>>>>>
>>>>>>> > Am I missing something or does this seem to be counter intuitive?
>>>>>>>
>>>>>>> if (!!window.myClass) ...
>>>>>>>
>>>>>>> !!  coerces  the  the  value to real boolean, which I like better
>>>>>>> than
>>>>>>> leaving it falsy/truthy.
>>>>>>>
>>>>>>> --Sandy
>>>>>>>
>>>>>>>
>>>>>> The MooTools Tutorial: www.mootorial.com Clientcide:
>>>>>> www.clientcide.com
>>>>>>
>>>>>> ------------------------------
>>>>>> View this message in context: Re: [Moo] Re: question about 
>>>>>> $defined<http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p2737159.html>
>>>>>> Sent from the MooTools Users mailing list 
>>>>>> archive<http://n2.nabble.com/MooTools-Users-f660466.html>at Nabble.com.
>>>>>>
>>>>>
>>>>>
>>>> The MooTools Tutorial: www.mootorial.com Clientcide: www.clientcide.com
>>>>
>>>> ------------------------------
>>>> View this message in context: Re: [Moo] Re: question about 
>>>> $defined<http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p2737486.html>
>>>> Sent from the MooTools Users mailing list 
>>>> archive<http://n2.nabble.com/MooTools-Users-f660466.html>at Nabble.com.
>>>>
>>>
>>>
>> The MooTools Tutorial: www.mootorial.com Clientcide: www.clientcide.com
>>
>> ------------------------------
>> View this message in context: Re: [Moo] Re: question about 
>> $defined<http://n2.nabble.com/-Moo--question-about-%24defined-tp2736644p2737607.html>
>> Sent from the MooTools Users mailing list 
>> archive<http://n2.nabble.com/MooTools-Users-f660466.html>at Nabble.com.
>>
>
>

Reply via email to