如何从javascript对象执行公共方法

时间:2017-03-06 09:54:05

标签: javascript jquery

我在访问外部文件中定义的函数时遇到问题,请提供一些建议如何修复以下内容。

网页代码:

  var be = null;
    $(document)
      .ready(function() {
        be = $("#article-content")
          .bootstrapeditor({
            //
          });

        // how can I call public method wizardWorkedOut from 
        // https://jsfiddle.net/f0rza/572q21fj/ ?
      });

https://jsfiddle.net/f0rza/jhotm90r/

外部脚本:

! function($) {

  var BootstrapEditor = function(element, options) {

    this.canDemolishEditor = true;

  };

  BootstrapEditor.prototype.wizardWorkedOut = function() {
    this.wizardWorkedOut();
  };

  BootstrapEditor.prototype = {
    constructor: BootstrapEditor,

    wizardWorkedOut: function() {
      console.log('bootstrap-editor instance: wizardWorkedOut');
      this.canDemolishEditor = true;
    }
  };

  $.fn.bootstrapeditor = function(option, val) {
    return this.each(function() {
      var $this = $(this),
        data = $this.data("bootstrapeditor"),
        options = typeof option === "object" && option;
      if (!data) {
        $this.data("bootstrapeditor",
          (data = new BootstrapEditor(this, $.extend({}, 
                                      $.fn.bootstrapeditor.defaults, options))));
      }
      if (typeof option === "string") data[option](val);
    });
  };

  $.fn.bootstrapeditor.defaults = {
    onRender: function(date) {
      return "";
    }
  };
  $.fn.bootstrapeditor.Constructor = BootstrapEditor;

}(window.jQuery);

https://jsfiddle.net/f0rza/572q21fj/

1 个答案:

答案 0 :(得分:0)

您可以通过创建BootstrapEditor实例进行访问,如下所示。

 //Creating an instance of bootstrapeditor
 var instance = new be.__proto__.bootstrapeditor.Constructor()
 //Access the methods in the prototype
 instance.wizardWorkedOut()