2017-03-06 56 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() 
相關問題