javascript - What is (function($) {...}) (jQuery)? -
this question has answer here:
i javascript newbie , came following code.
(function($){ if(!document.defaultview || !document.defaultview.getcomputedstyle){ var oldcurcss = jquery.curcss; jquery.curcss = function(elem, name, force){ if(name !== 'backgroundposition' || !elem.currentstyle || elem.currentstyle[ name ]){ return oldcurcss.apply(this, arguments); } var style = elem.style; if ( !force && style && style[ name ] ){ return style[ name ]; } return oldcurcss(elem, 'backgroundpositionx', force) +' '+ oldcurcss(elem, 'backgroundpositiony', force); }; } })(jquery);
what function($) {...} (jquery)
?
ps: don't quite code for... if possible please give hint on it.
it's self-executing anonoymous function called jquery
argument, gets renamed $
inside function signature.
since don't know if jquery.noconflict()
has been used in page (especially if writing distributable code plugins), way can use shorthand $
inside function safely.
who wrote stupid beacuse he's using jquery
inside function anyway : )
it prevents variables polluting global namespace , encapsulates them making them unaccessible outside function.
Comments
Post a Comment