2
Since a static method will not be an object method I can have a static method with the same name as a property defined within the constructor?
2
Since a static method will not be an object method I can have a static method with the same name as a property defined within the constructor?
2
Yes, it 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());
2
This is indeed possible, but they cannot be accessed at the same time for obvious reasons.
The method defined as static property is no longer accessible when the class is instantiated. And the method defined as class property/method is only accessible after instantiation.
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());
Browser other questions tagged javascript method property nomenclature
You are not signed in. Login or sign up in order to post.