JQ闭包 / JS插件开发
JS插件开发demo
JavaScript Code复制内容到剪贴板
- //plugin.js
- ;(function(undefined) {
- "use strict"
- var _global;
- function result(args, fn) {
- var argsArr = Array.prototype.slice.call(args);
- if(argsArr.length > 0) {
- return argsArr.reduce(fn);
- } else {
- return 0;
- }
- }
- var plugin = {
- add: function() {
- return result(arguments, function(pre, cur) {
- return pre + cur;
- });
- },
- sub: function() {
- return result(arguments, function(pre, cur) {
- return pre - cur;
- });
- },
- mul: function() {
- return result(arguments, function(pre, cur) {
- return pre * cur;
- });
- },
- div: function() {
- return result(arguments, function(pre, cur) {
- return pre / cur;
- });
- },
- sur: 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);
- }
- }());
- //输出结果
- with(plugin){
- console.log(add());
- console.log(add(2, 1)); //3
- }
闭包写法,var 表示只能在函数内使用,_self开放给外部使用的方法
(function($){
var WidgetPlugin = function(){
var _self = this;
var ses = {
'show_plug' : '#plugin .modal-body .form-horizontal',
};
/* 渲染配置表单 */
_self.renderPluginConfigForm = function(data){
var index = layer.load(1);
$.post("/admin/plan/com/plugin/view", data,
function(rep){
layer.close(index);
if(rep.code == 200){
$(ses.show_plug).html(rep.data);
}else{
layer.msg("拉取失败,请重试");
}
}, "json");
}
/* 保存插件配置 */
_self.saveWidgetConfig = function(data){
var index = layer.load(1);
$.post("/admin/plan/com/plugin/edit", data,
function(rep){
layer.close(index);
var content= rep.data;
layer.msg(content, {
time: 1000
}, function(){
document.location.reload();
});
}, "json");
}
_self.insertInstance = function(data){
layer.open({
title:'信息',
content: "是否确定启用实例",
btn: ['是', '否'],
yes:function(index){
$.post("/admin/plan/com/plugin/create", data,
function(rep){
if(rep.code == 200){
layer.msg(rep.data, {
time: 1000
}, function(){
document.location.reload();
});
}else{
layer.msg(rep.data);
}
}, "json");
}
});
};
/* 初始化加载layer插件 */
var init = function(){
layui.use(['layer'], function(args){
var layer = layui.layer;
});
}
init();
}
//-- Process event --
var widgetHandler = new WidgetPlugin;
$(".editPlug").on('click', function() {
var id = $(this).attr("instance-id");
var csrfToken = $('meta[name="csrf-token"]').attr("content");
var data = { "id": id,"_csrf":csrfToken };
widgetHandler.renderPluginConfigForm(data);
});
$("#up_btn").on('click', function() {
var data = $("#pluginForm").serialize();
widgetHandler.saveWidgetConfig(data);
});
$(".addInstance").on('click', function() {
var id = $(this).attr("data-widget-id");
var csrfToken = $('meta[name="csrf-token"]').attr("content");
var data = {"widgetId":id,"_csrf":csrfToken};
widgetHandler.insertInstance(data);
});
})(jQuery);
闭包函数如何访问外部变量:
JavaScript Code复制内容到剪贴板
- <script>
- // var name = "The Window";
- // var object = {
- // name : "My Object",
- // getNameFunc : function(){
- // return function(){
- // return this.name;
- // };
- // }
- // };
- // alert(object.getNameFunc()()); // 访问闭包内外部元素
- var name = "The Window";
- var object = {
- name : "My Object",
- getNameFunc : function(){
- var that = this;
- return function(){
- return that.name;
- };
- }
- };
- alert(object.getNameFunc()());// 只能访问内部的元素
- </script>