The 'trick' is the brackets after Jorn's function definition.

(function(){
...
})(); <-- notice the brackets

It's an anonymous function that gets invoked immediately, then is gone 
to the big garbage collector in the sky. It's not necessary to attach 
the function to jQuery or invoke the function later (since the query 
string won't change) -- we can just store the object it returns on 
jQuery for later :-)

Running all the code inside an anonymous function avoids potential 
naming conflicts by keeping all the variables from cluttering out of the 
global namespace (i.e. q is local to the function, and doesn't hang 
around as window.q, etc.)

Hope that helps,
Luke

Stephen Woodbridge wrote:
> Hi Jörn,
> 
> I seem to be missing something (so much to learn!). Your note creates an 
> anonymous function that assigns its result to jQuery.query, but where 
> does the function get attached and how does it get invoked, like in 
> Luke's example.
> 
> Thanks,
>    -Steve
> 
> Jörn Zaefferer wrote:
>> Luke Lutman schrieb:
>>> I was thinking of something a little fancier ;-)
>>>
>>> jQuery.query = function() {
>>>     var r = {};
>>>     var q = location.search;        
>>>     q = q.replace(/^\?/,''); // remove the leading ?        
>>>     q = q.replace(/\&$/,''); // remove the trailing &
>>>     jQuery.each(q.split('&'), function(){
>>>             var key = this.split('=')[0];
>>>             var val = this.split('=')[1];
>>>             // convert floats
>>>             if(/^[0-9.]+$/.test(val))
>>>                     val = parseFloat(val);
>>>             // ingnore empty values
>>>             if(val)
>>>                     r[key] = val;
>>>     });
>>>     return r;
>>> };
>>>
>>> Then you could use it like so:
>>>
>>> http://www.bork.com?foo=helloworld&bar=0.333&;
>>>
>>> $.query()['foo']; // helloworld (string)
>>> $.query()['bar']; // 0.333 (number)
>>>   
>> In that case, you should just assign the result to jQuery.query, eg:
>> (function() {
>>
>>      jQuery.query = r = {};
>>      var q = location.search;        
>>      q = q.replace(/^\?/,''); // remove the leading ?        
>>      q = q.replace(/\&$/,''); // remove the trailing &
>>      jQuery.each(q.split('&'), function(){
>>              var key = this.split('=')[0];
>>              var val = this.split('=')[1];
>>              // convert floats
>>              if(/^[0-9.]+$/.test(val))
>>                      val = parseFloat(val);
>>              // ingnore empty values
>>              if(val)
>>                      r[key] = val;
>>      });
>>
>> })();
>>
>> Considering that location.search does not change. I hope I'm not 
>> confusing something.
>>
>> -- Jörn
>>
>> _______________________________________________
>> jQuery mailing list
>> [email protected]
>> http://jquery.com/discuss/
> 
> 
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to