I want to create an object with variable key with the help of class instance. I have tried by the following code
class ACL {
constructor(key){
this[key] = {
read: false,
write: false,
delete: false
}
}
setRoleReadAccess(key, value){
[key] = {
read: value,
}
}
setRoleWriteAccess(key, value){
[key] = {
write: value,
}
}
setRoleDeleteAccess(value){
[key] = {
delete: value,
}
}
}
const userACL = new ACL("1234");
userACL.setRoleReadAccess("1234", true);
userACL.setRoleWriteAccess("1234", true);
console.log(userACL);
But i am getting this error UnhandledPromiseRejectionWarning: TypeError: Invalid attempt to destructure non-iterable instance
i am expecting the following console output
ACL {
"1234": {
read: true,
write: true,
delete: false
}
}
Can anyone tell me how can resolve this
[this.key]in the constructor?[this.key]tothis[key]and tell me what happens