Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!