When writing jQuery plug-ins, note that writing jQuery plug-ins
Write the jQuery plug-in, and pay attention to it (continuous addition ).
Support UMD
Currently, the front-end development focuses on modularization, so jQuery plug-ins should be both modular.
There are several modular modes: AMD, CommonJs, and UMD.
AMD (Asynchronous Module Definition)
Asynchronous module definition. It can be asynchronously loaded or dependent on other modules. supported libraries include Require. js and Sea. js.
Example:
// xxx-plugin.jsdefine(['jquery'], function ($) { function myFunc(){}; return myFunc;});
CommonJs
Javascript can be defined as a Node module.
Example:
var $ = require('jquery');function myFunc(){};module.exports = myFunc;
UMD (Universal Module Definition)
To be compatible with AMD and CommonJs styles, UMD emerged.
Code:
(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD define(['jquery'], factory); } else if (typeof exports === 'object') { // Node, CommonJS-like module.exports = factory(require('jquery')); } else { // Browser globals (root is window) root.returnExports = factory(root.jQuery); }}(this, function ($) { function myFunc(){}; return myFunc;}));