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 instances = [];
        this.each(function(key, value){
            if ($(this).data('myclass')) {
                return $(this).data('myclass');
            }
            
            var instance = new MyClass(this);
            $(this).data('myclass', instance);
            instances.push(instance);
        });
        if (instances.length == 1)
            return instances[0];
        return instances;
    };
    
})(jQuery);


var myObject = $('#test').myPlugin();

alert(myObject.getMyValue());
myObject.setMyValue('Goodbye World');
alert(myObject.getMyValue());