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

129
Visualizações
Javascript function inside polymer is not a function error

I have a javascript function defined in polymer as such: myTest(arg) {<do stuff here>}

Inside this function, based on some conditions, I want to change the onclick of a button defined in the polymer template to some other function. For this I'm doing document.getElementById('myButton').onclick = function() {myTest('someArg')};

The problem I'm having is that when the above line gets invoked I'm getting an error

Uncaught TypeError: myTest is not a function.

If I try to use the function keyword when declaring the function the compiler complains with

Unexpected token. A constructor, method, accessor, or property was expected.

What's the correct syntax to for changing the button's onclick function?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's hard to say for sure without seeing the the full source code example, but it sounds like the problem you're facing is related to JavaScript not inferring this as the context for calling instance methods in the same way that e.g. Java does. To make things more complicated, this wouldn't have the expected value inside the function that you create for the click listener.

One way around that would be to use an arrow function since they aren't redefining the execution context:

document.getElementById('myButton').onclick = () => { this.myTest('someArg') };

Another alternative is to explicitly capture this into a separate variable and then use that variable inside the function:

var self = this;
document.getElementById('myButton').onclick = function() { self.myTest('someArg') };
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