Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
class with static and instance methods

The goal - create function, that behaves like class with static methods and at the same time can be used to create instanses. All this resides in dedicated namespace. I am looking for "standard" ptototype approach. And is it good practice in general? I tried to avoid use reference to "class" by function name. If I would have 10 or more static methods it could be difficult. In first example I used _self, and in another method defineProperties.

var MyNamespace = (function() {
    
    function MyClass0(e) {
        this.e = e;
        var _self = MyClass0;
        
        _self.prototype = {
            instanceMethod: function() {
                
            }
        }
        _self.staticMethod = function() {
            
        }
        if (!e) {
            return _self;
        }
    }
    function MyClass1(e) {
        this.e = e;
        MyClass1.prototype = {
            instanceMethod: function() {
                
            }
        }
        var properties = {
                staticMethod: {
                    value: function() {
                }
            }
        };
        Object.defineProperties(MyClass1, properties)
        if (!e) {
            return MyClass1;
        }
    }

    return {
        MyClass0: MyClass0(), // use _self
        MyClass1: MyClass1() // use defineProperties
    }
})();
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda