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

95
Visualizações
how to access class this inside jquery callback es6
    events ( node ) {

        let thisclass = this;

        node.on('mouseover', ( d, i ) => {
            thisclass.animateParentChain( d );
        });

        node.on('mouseout', (d, i) => {
            d3.select(this).select("circle").attr("r", 2.5);
            thisclass.unAnimateParentChain( d );
        });

        node.on('click', (nd, i) => {
            thisclass.persistsAnimateParentChain( nd );
            thisclass.loadModal(nd);
        });
    }

The above is a method in an es6 class. The original script looked like this:

events: function ( node ) {
        node.on('mouseover', function ( d, i ) {
            app.radialD3.animateParentChain( d );
        });
        node.on('mouseout', function(d, i){
            d3.select(this).select("circle").attr("r", 2.5);
            app.radialD3.unAnimateParentChain( d );
        });
        node.on('click', function(nd, i){
            app.radialD3.persistsAnimateParentChain( nd );
            app.radialD3.loadModal(nd);
        });
    },

The new es6 method can be rewritten using the function keyword eg:

events ( node ) {

        let thisclass = this;

        node.on('mouseover', function ( d, i ) {
            thisclass.animateParentChain( d );
        });

        node.on('mouseout', function (d, i) {
            d3.select(this).select("circle").attr("r", 2.5);
            thisclass.unAnimateParentChain( d );
        });

        node.on('click', function(nd, i){
            thisclass.persistsAnimateParentChain( nd );
            thisclass.loadModal(nd);
        });
    }

Changing to use the function keyword and things work. Without, the script has issues with this keyword.

I am reasonably new to the es6 stuff, but what is the "proper" way of writing this. In the callback i need this to be the html element and not the class, but at the same time the callback needs to access methods of the class. Using the function keyword seems to be taking a step backward, as does setting the current class into a variable :/

How do you differentiate between the this's ?

over 4 years ago · Santiago Trujillo
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