Since a static method is not going to be an object method, can I have a static method with the same name as a property defined inside the constructor?
Since a static method is not going to be an object method, can I have a static method with the same name as a property defined inside the constructor?
This is in fact possible, but can not for obvious reasons be accessed at the same time.
The method defined as static property is no longer accessible when the class is instantiated. And the method defined as property / method of the class is only accessible after instantiated.
class MinhaClasse {
constructor(nome) {
this.nome = nome;
console.log('Classe', nome);
}
static metodoEstatico(nome) {
return 'Método estático de ' + nome;;
}
metodoEstatico() {
return this.constructor.metodoEstatico(this.nome + '...');
}
}
var Fulano = new MinhaClasse('Fulano');
console.log(MinhaClasse.metodoEstatico('Beltrano'));
console.log(Fulano.metodoEstatico());
Yes, you can, there is no confusion between static members and instance members.
class Teste {
static teste() {
return 1;
}
constructor() {
this.teste = 2;
}
}
var x = new Teste();
console.log(x.teste);
console.log(Teste.teste());