plugin插件↓↓javascript
;(function (underfined) { "use strict" var _global; //插件函數 /* var plugin = { add: function (n1, n2) { return n1 + n2; }, sub: function (n1, n2) { return n1 - n2; }, mul: function (n1, n2) { return n1 * n2; } } */ //插件API設置參數 function result(args,fn) { var str = Array.prototype.slice.call(args); if (str.length > 0){ return str.reduce(fn); } else { return 0; } } var plugin = { add: function () { //加 return result(arguments,function (pre,cur) { return pre + cur; }); }, mul: function () { //減 return result(arguments,function (pre,cur) { return pre + cur; }); } } //最後將插件函數暴露給全局 _global = (function () { return this || (0, eval)('this'); }()); if (typeof module !== 'undefined' && module.exports) { module.exports = plugin; } else if (typeof define === 'function' && define.amd) { define(function () { return plugin; }); } else { !('plugin' in _global) && (_global.plugin = plugin); } }());
htmlhtml
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="plugin.js"></script> </head> <body> <script> //引入插件,盡情使用plugin對象吧。。。。。。 with(plugin){ document.write(add(2,1));// 3 document.write(mul(7,7));//49 } </script> </body> </html>