JQuery has two ways of encapsulating code $.extend and $.fn.extend, which we also call encapsulation Plug-ins
$.extend DEMO
// encapsulates function (options) { Span style= "color: #339966;" >//options can also be multiple parameters a, b, c var name= options.name| | "; var words = Options.words | | "; document.write (name + ' say: ' + words); }}) // call options is multiple arguments, $.say (a, b, c); Name: ' Tom ' ' Hello ' }); // output Tom Say:hello
$.fn.extend DEMO
<div id= "man" >man</div>// package $.fn.extend ({talk :function( Options) { var defaults = { words:' something ' } var opts = $.extend (defaults, options); var obj = $ (this); obj.html (defaults.words); }}) // Call $ (' #man '). talk ({words: ' Hi '});
thus, $.extend can be called directly, $.fn.extend requires an object-based invocation of the
JQuery Extend implementing Code encapsulation