1
I created the Employee and Director classes, being Employee Inheritor Manager(daughter), but when instantiating a Director object I got the following error: Syntaxerror: Private field '#bonus' must be declared in an enclosing class.
Replaces the "#" used in private attributes with "_" and it worked. Are private attributes with "#" not inherited? Someone knows the reason why it doesn’t work?
NOTE: I am using Node.js v.14.2.0 to interpret the code.
Below are the classes in question:
// index.js
import { Funcionario } from "./funcionario/Funcionario.js";
import { Gerente } from "./funcionario/Gerente.js";
import { Diretor } from "./funcionario/Diretor.js";
const diretor = new Diretor(11122233345, "Roberto", 2000);
export class Funcionario {
#bonificacao = 1;
#cpf;
#nome;
#salario;
#senha;
constructor(cpf, nome, salario) {
this.#cpf = cpf;
this.#nome = nome;
this.#salario = salario;
if (this.constructor == Funcionario)
throw new Error(
"Não é permitida a instanciação da classe Funcionario diretamente."
);
}
get senha() {
return this.#senha;
}
cadastrarSenha(senha) {
this.#senha = senha;
}
}
import { Funcionario } from "./Funcionario.js";
export class Diretor extends Funcionario {
constructor(cpf, nome, salario) {
super(cpf, nome, salario);
this.#bonificacao = 2;
}
}
Thanks for the help. But I want Director.js to inherit the bonus variable from Funcionario.js, thus, if there is no "this. #bonus = 2", I want the default value to be the one set in the mother class. If I set #bonus in Director.js, the value will be Undefined. Is there a prefix that determines that a variable is protected, just as # determines that the variable is private? Otherwise, I should use as the default prefix "_" leaving the public variables and using encapsulation for their protection, or is there some other way around it?
– Luan Lima
Private Attributes/Fields is a new Feature, and for many years devs had to simulate this feature. Unfortunately, in your case, there is no way to declare protected Fields.
– Mestre San