Recently, I made the decision to redevelop a jQuery plugin using vanilla JavaScript. However, I have encountered an issue with getting the public methods to function properly. Despite all other logic working correctly, the public methods are not responding as expected. As an example, I have included the update method below.
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory(root));
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.Plug = factory(root);
}
})(typeof global !== "undefined" ? global : this.window || this.global, function(root) {
var _settings;
var defaults = {
// default values
}
function runPlugin(){
alert('it works')
}
var Plug = function(options) {
var Plug = {};
var settings;
_settings = extend(settings || defSettings, options || {});
var init = (function() {
//init logic
runPlugin();
...
}());
Plug.update = function(msg){
alert(msg)
}
}
return Plug;
});
var plug = new Plug({//settings...}); //works
var plug = new Plug().update('test'); //does not work