I wrote a simple code:
const secure = new class {
#privateProperty = 4;
#privateMethod() {
console.log( 'The property ' + this.#privateProperty + ' should not be accessible outside this class' );
}
}
If it is immediately followed by the syntax below
secure.#privateMethod();
I get an error which says Uncaught SyntaxError: Private field '#privateMethod' must be declared in an enclosing class
However, If I don't immediately call the secure.#privateMethod()
and then go to developer tool - console and write the syntax there, it outputs:
The property 4 should not be accessible outside this class
Is there a special reason why this is happening?