如何在javascript中使用工厂模式?

时间:2016-12-07 15:01:53

标签: javascript jquery

我对这个模式了解不多,所以我尝试检查一些Jquery插件并遵循他们的结构但是我想重用这个插件。

这是代码

(function (factory) {
  if (typeof define === 'function' && define.amd) {

    // AMD. Register as anonymous module.

    define(['jquery'], factory);
  } else if (typeof exports === 'object') {
    // Node / CommonJS
    factory(require('jquery'));
  } else {
    // Browser globals.
    factory(jQuery);
  }
}) (function ($) {
  'use strict';

  var console = window.console || { log: function () {} };

  function Testing($element, $els) {

    this.$Form = $($elementss);
    this.$Input = this.$Form.find($els);
    this.init();
  }

    Testing.prototype = {
    constructor: Testing,

    init: function () {
      this.addListener();
    },

    addListener : function () {
      this.$Input.on('change', $.proxy(this.changeIt, this));
    },

    changeIt: function (e) {

        console.log(e)

    }    
  };

  $(function (e) {

    return new Testing($('#form'), '.inputClass');

  });

});

如上所述我可以在返回新测试($('#form'),' .inputClass'); 上调用它但是我怎么能如果我想与其他选择器一起使用它。

0 个答案:

没有答案
相关问题