2017-03-06 34 views
0

执行公共方法与访问外部文件中定义的功能的问题,请给一个忠告,我该如何解决以下。我如何我可以从JavaScript对象

页代码:

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/

+0

您必须创建'BootstrapEditor'实例,然后才能调用它的方法,例如'wizardWorkedOut' – hindmost

+0

对象初始化即使我得到一个错误:http://take.ms/re9FU – f0rza

回答

0

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

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