JSFiddle - React, Tailwind, and code Playground
by Tyriar
HTML
<div id="test"></div>
JavaScript
(function ($) {
var MyClass = function (element) {
var myValue = 'Hello World';
this.getMyValue = function() {
return myValue;
};
this.setMyValue = function(value) {
myValue = value;
};
return this;
};
$.fn.myPlugin = function() {
var instance;
this.each(function(key, value){
if ($(this).data('myclass')) {
return $(this).data('myclass');
}
instance = new MyClass(this);
$(this).data('myclass', instance);
});
return instance;
};
})(jQuery);
var myObject = $('#test').myPlugin();
alert(myObject.getMyValue());
myObject.setMyValue('Goodbye World');
alert(myObject.getMyValue());