The first method:
In many jquery-based or zepto-based plugins, the immediate function execution is preceded by a ";" This symbol.
This is to prevent the other plugins in the front from shutting down properly.
When the function execution is executed immediately, Jquery,window is usually passed in. As an example:
;(function ($,window,undefined) {//...}) (Jquery,window)
Window is passed in as a local variable rather than a global variable, which speeds up the parsing process and minimizes the impact.
Undefined is not passed in so that it can be ensured that this undefined is a real undefined. Because ECMASCRIPT3 in the undefined can be modified, ECMASCRIPT5 can not be modified.
The following is a specification for a custom jquery plugin:
;(function ($,window,undefined) {var pluginname = "Chaojidan", defaults = {name: "Dandan"};function Plugin (element, Options) {this.element = Element;this.options = $.extend ({}, defaults,options); this._name = Pluginname;this._defaults = Defaults;this.init ();} Plugin.prototype.init = function () {//Initialize plug-in};$.fn[pluginname] = function (options) { //real plug-in wrapper, prevents multiple instances from appearing return This.each (function () {if (!$.data (this, "Plugin_" +pluginname)) {$.data (this, ' Plugin_ ' +pluginname, new plugin (this, options));}) (Jquery,window)
Call this plugin:
$ ("#elem"). Chaojidan ({name: "Xiaoxiao"});
The second method:
;(function ($,window,undefined) {var myObject = {init:function (options, elem) {this.options = $.extend ({}, This.options, options); This.elem = elem;this. $elem = $ (elem); This._build (); return This;},options:{name: "Dandan"},_build:function ( {},mymethod:function () {}};if (typeof object.create! = "function") {object.create = function (o) {function F () {} F.prototype = O;return new F ();}} $.plugin = function (name, object) {$.fn[name] = function (options) {return This.each (function () {if (!$.data (This,name)) { $.data (This,name,object.create (Object). Init (Options,this))}});}} $.plugin ("Chaojidan", MyObject);}) (Jquery,window);
Call Mode:
$ ("#elem"). Chaojidan ({name: "Xiaoxiao"});
For the two methods above, when we define the plug-in, we pass the object literal with the default value to $.extend (). However, if we want to customize the object literal of this default value, that is, the user can override this default object literal, then how do we write it?
It's really very simple, we just have to assign this default object literal to this value.
$.fn.pluginname.options = {
Name: "Dandan"
}
This allows the user to override the global default configuration to achieve the flexibility of the plug-in construction.
Come on!
jquery Plugin Authoring Specification