Customizing the build jquery Plug-in Experiment (2)

Source: Internet
Author: User

Simple.

Test.js

;(function ($) {  $.fn.getdata=function (options) {  return this.append (options);  }) (JQuery);

Test.html

<! DOCTYPE html>


A little more complicated.

Test2.js

 (function ($) {debugger; Within our plugin container, create a public variable to build a private method var privatefunction = function () {//code here}//Create an object by literal, store the common party we need var methods = {//In the literal object defines each individual method Init:function () {//For better flexibility, for each of the selectors from the main function and into each method Separate elements execute code return This.each (function () {//Create a jquery object for each individual element var $this = $ (th                 IS);            Execute code//For example: Privatefunction ();        }); }, Destroy:function () {//Executes method return This.each () for each element of the selector (function () {//Execute generation        Code});     }    }; $.fn.pluginname = function () {///Get our method, unfortunately, if we use function (method) {} to implement, this will ruin everything var method = Arguments[0]         ;            Verify if the method exists if (Methods[method]) {//If the method exists, store it for use//NOTE: I'm doing this to make it easier to use each ()         method = Methods[method]; If the method does not exist, verify that the object is an object (JSON object) or that method methods are not passed in} else if (typeof (method) = = ' object ' | |        !method) {//If we pass in an object parameter, or there is no argument at all, the Init method is called method = Methods.init; } else {//If the method does not exist or the parameter does not pass in, an error is reported.            The method that needs to be called is not called correctly $.error (' method ' + method + ' does not exist on Jquery.pluginname ');        return this;     }//Call our chosen method//again notice how we transfer each () from here to return Method.call (this) on each individual method; }}) (JQuery);


Test2.html

<! DOCTYPE html>
Special attention to the second case, if you read my previous principle is not clear, please source debugging, step by step to see the jquery source design mode.





Customizing the build jquery Plug-in Experiment (2)

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.