Use a conditional shortcut:
(function($){ !$.test && $.test= function(){ alert('test'); } })(jQuery); jQuery.test();
!$.test evaluates to true if $.test not defined, and the code with permissions after && is executed. Otherwise, it will simply skip the part. Another useful template for this might look like this:
$.test = $.test || function() { alert('test'); }
It does the same thing really. If $.test already defined, it is used, otherwise assign an anonymous function to $.test . Both versions can (and probably should) be optimized, but not just check if they were defined, but also if they are of type function .
jAndy
source share