dimanche 22 novembre 2015

Generating different Objects with each js plugin initilization

I have following code.

(function (window) {

    window.plug = function() {

        this.version = '1.0.0';

        this.value = arguments[0];

        this.init();

    }

    plug.prototype.init = function () {

        var a = document.createElement('div');
        a.textContent = 123;
        self = this;
        a.addEventListener('click', function (e){
            console.log(self.value);
        });

        document.body.appendChild(a);

    };

}(window));

When user initializes the plugin it should create different object per each. for an instance var plug = new plug(1); var plug = new plug(3); var plug = new plug(5); is called three times, when the user clicks on generated div tag, it should log the three different valuees passed.

And how do you implement a jquery style plugin initialization method in vanila js

Eg: plug({attr1: 1, arrr2: 2});

if someone could give me a detailed explanation i really appreciate that.

demo

Aucun commentaire:

Enregistrer un commentaire