I am experimenting with a new pattern,
where each newly created Card element
uses the constructor
scope to store (private) variables:
pseudo code:
class CardtsCard extends HTMLElement {
constructor(){
let private = 666;
Object.assign(this,{
getPrivate : () => console.log(private)
});
}
get private(){
//can not access scope in constructor
return 42;
}
}
So:
el.getPrivate(); // outputs 666
el.private; // outputs 42
I have loads of getters and setters
and sticking data ON my elements with
this.whatever=value
feels a bit weird.
I can extend it to:
class CardtsCard extends HTMLElement {
constructor(){
let private = new Map();
Object.assign(this,{
setPrivate : (data,value) => private.set(data,value),
getPrivate : (data) => private.get(data)
});
}
}
Question: I am no JS scope expert, are there drawbacks?