How to get access to private field via square brackets in JavaScript

It looks like it is a problem to hand over a string which has a special meaning.

If really necessary, you evaluate a string.

class Test {
    #field = 'foo';

    get field() {
        return eval('this.' +'#field');
    }
}

console.log(new Test().field)

Leave a Comment