lundi 29 mai 2017

ES6 Map an object to a decorator

I'd like to map an object with properties (key) to a decorator (value). I'd like to use a Weak Map if possible. I have a solution that is working using a string, which is fine except that Weak Maps do not accept strings as keys. Is this possible with a Map or a WeakMap?

'use strict';

class Accordion {

    constructor() {}

}

let Decorators = new Map();

Decorators.set({nodeName: 'tag-name-here', component: 'accordion'}, (client) => { return new Accordion(client) });

class Client {

    constructor() {

        let key =  {nodeName: 'tag-name-here', component: 'accordion'}
        let decorator;

        if (Decorators.has(key)) {

            decorator = Decorators.get(key)(this);

        }

        console.log(decorator); //undefined, unless I use a string as a key.
    }
}

new Client();

Aucun commentaire:

Enregistrer un commentaire