What does the jquery statement "var collection = jQuery([1]);" mean? -


on page 109 of book "learning javascript design patterns", there code sample confused me.

jquery.single = (function( o ){      var collection = jquery([1]);  // <-- want ask line     return function( element) {           // give collection element          collection[0] = element;           // return collection          return collection;     } })(); 

the use of function this:

$('div').on('click', function() {      var html = jquery.single( ).next().html();      console.log( html ); }); 

update: answering. checked out original code source author page 76 bytes faster jquery

var collection = jquery([1]); // fill 1 item, make sure length === 1 

now understand. wish author of book "learning javascript design patterns" add comment too, when cited code sample.

jquery([1]) passing array 1 entry contains integer 1 jquery, return wrapper array containing jquery prototype methods.

it later assigns element argument variable same array , returns entire jquery instance.

from looks of it, whole function injects single element re-used jquery object , returns it.

it presumed speed things , when count millions of iterations on second, in opinion clear case of micro-optimization can bite in ass during long debug hours. — perfection kills.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -