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

196
Visualizações
Can a getter return a thunk or value depending on the type call call (virt. property or function)?

I know this a weird question, but I'll ask it anyway.

The pseudo code function below provides a virtual property getter. What it should do is this:


> const calc = Calculator(3)
> calc.multiple    // returns getter result directly
6
> calc.multiple(4) // returns thunk result, thus behaves like a function
12

Here's my pseudo code:

function Calculator(num) {

  return {
    get multiple() {
      if (isFunctionCalled()) {   // isFunctionCalled is an
                                  // imaginary function check
        return (mult = 3) => {
          return mult * num;
        };
      }
      // getter called, default calulation
      return 2 * num;
    },
  };
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No, there's no way to know from within multiple whether the value it returns, when used, will be used as a function or used as a non-function.

You could always return a function and provide the immediate value as a property on that function, but you can't return a number in one case but a function in another.

You can also play games with overriding the [Symbol.toPrimitive], valueOf, and/or toString operations on the function you return, but that tends to produce surprising results when used in the wild.

You're probably best off differentiating by name, multiple vs. getMultiple, or multiple vs. multipleThunk, etc.

about 4 years ago · Juan Pablo Isaza Relatório
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