mardi 25 février 2020

Return this in static function class

I have two objects, I want to make singleton class and then call them like this Row.init([789]).render(). In the first case I try and it works:

var Row = (function (options) {
        console.log(options);
        this.data = [123];
        this.render = function () {console.log(this.data);}
        this.init = function (data) {
            this.data = data;
            return this;
        }
        return this;
    })();

but in the second case:

class Row {
    constructor() {
        this.data = {};
    }
    static init(data) {
        this.data = data;
        return this;
    }
    fill() {
        return this;
    }
    render() {
        console.log(this.data);
    }
}

It cannot call render method, I think it doesnt return this for me. How do I fix it? And in the first case, how do I pass options parameter?

Aucun commentaire:

Enregistrer un commentaire