Js plug-in writing call



<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<script src="http://cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
</head>
<body>
<button class="layui-btn demo1">1</button>
<button class="layui-btn demo2">2</button>

<script>


var my = {
  defaults: {
    id: ".fast_pay",
    client_id: "", //ID
    sign:"",//
    qr_load:null,
    success: null // 
  },
  set: function(options) {
    var that = this;
    that.settings = $.extend({}, that.defaults, options);
    return that;
  },
  int: function(options) {
    var that = this;
    var that = this.set(options);
    settings=that.settings;
    $(document).on("click", settings.id, function() {
      that.shows_qr($(this));
    });
  },
  shows_qr: function(item) {
    var that = this;
    if (typeof settings.success === "function") {
      settings.success(item);
      return false;
    }
  }
}



my.int({
id:".demo1",
success:function(data){
  alert("demo1");
  console.log("demo1"+data);
}
});

my.int({
  id:".demo2",
  success: function(data) {
      alert("demo2");
    console.log("demo2"+data);
  }
});




</script>
</body>
</html>

Why do you always call back to demo2commands!

how should I write this way?

Click the button.. Make a callback!

Mar.18,2021

var My = function(options){
    ...
}

var my1 = new My(...)
var my2 = new My(...)

your settings is a global variable, so naturally there is only one callback.

  1. pass the callback to shows_qr
  2. bind different callbacks, using upstairs methods, closures, or dictionaries.
Menu