jquery Plugin Authoring Specification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.