Can a static method have the same name as a property?

2

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?

    
asked by anonymous 22.08.2017 / 17:58

2 answers

2

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());
    
22.08.2017 / 18:08
1

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());
    
22.08.2017 / 18:08