Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
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 ?

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda