如何从javascript对象执行公共方法
我在访问外部文件中定义的函数时遇到问题,请给出一条建议如何修复以下问题 页面代码:如何从javascript对象执行公共方法,javascript,jquery,Javascript,Jquery,我在访问外部文件中定义的函数时遇到问题,请给出一条建议如何修复以下问题 页面代码: var be = null; $(document) .ready(function() { be = $("#article-content") .bootstrapeditor({ // }); // how can I call public method wizardWorkedOut
var be = null;
$(document)
.ready(function() {
be = $("#article-content")
.bootstrapeditor({
//
});
// how can I call public method wizardWorkedOut from
// https://jsfiddle.net/f0rza/572q21fj/ ?
});
外部脚本:
! 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);
您可以通过创建BootstrapEditor的实例进行访问,如下所示
//Creating an instance of bootstrapeditor
var instance = new be.__proto__.bootstrapeditor.Constructor()
//Access the methods in the prototype
instance.wizardWorkedOut()
您必须创建一个
BootstrapEditor
的实例,然后您可以调用它的方法,例如wizardWorkedOut
,即使在初始化对象之后,我也会得到一个错误: