JavaScript How to acces to parent of “this” on JavaScript? access parent this javascript,access parent this,react access parent

I have a problem programming object-oriented in Javascript. I have the following:

class Foo{
    constructor(){...}
    ...
    a_needed_method(){...}
    ...
    a_method(){
        ...
        jsObject.on("click",function(params){
            this.a_needed_method();
        });
    }  
}

The problem is, if I call the a_needed_method method using this there, then this is referencing the anonymous function that controls the onclick event. How I can call a_needed_method() from this anonymous function?

Answer:1

ES6

You can use an arrow function to preserve the lexical this:

jsObject.on("click", params => {
  this.a_needed_method();
});

ES5

You can use the good old that = this:

var that = this;
jsObject.on("click", function (params) {
  that.a_needed_method();
});
Answer:2

In your constructor you can say:

constructor(){
  this.a_needed_method = this.a_needed_method.bind(this);
}
Answer:3

I have an application where there could be anywhere between 1 - 30 POST ajax calls happening in parallel. Also these calls are being made to the same method in the controller. But when there are more ...

I have an application where there could be anywhere between 1 - 30 POST ajax calls happening in parallel. Also these calls are being made to the same method in the controller. But when there are more ...

I have attached a picture of the click action I want to run from the Chrome console, with various values passed. The button is highlighted in gray top right in the inspect element. It is the ...

I have attached a picture of the click action I want to run from the Chrome console, with various values passed. The button is highlighted in gray top right in the inspect element. It is the ...

  1. access angular controller from console
  2. access angular controller from javascript
  3. access angular controller variable in javascript
  4. access angular controller
  5. access controller scope angularjs
  6. access angularjs controller from console
  7. angular access controller scope from outside
  8. angular access controller from another module
  9. angular access controller variable
  10. angular access controller from directive
  11. angular access controller from html
  12. angular access controller variable from directive
  13. angular access controller method
  14. angular access controller functions
  15. angularjs access controller scope from directive
  16. angularjs access controller from directive
  17. angularjs access controller scope from service
  18. angularjs access controller from javascript
  19. angularjs access controller variable from directive
  20. angularjs access controller

I need a regex for javascript for matching "{any group of chars}" <-- where that last " is not preceeded by a \ examples: ... foo "bar" ... => "bar" ... foo"bar\"" ... => "bar\"" ... foo ...

I need a regex for javascript for matching "{any group of chars}" <-- where that last " is not preceeded by a \ examples: ... foo "bar" ... => "bar" ... foo"bar\"" ... => "bar\"" ... foo ...

I'm trying to accomplish a couple things with JS/CSS that I can't figure out. Here's what I've got so far: http://codepen.io/melissall/pen/PPWPQE I want the headline to be centered in the image until ...

I'm trying to accomplish a couple things with JS/CSS that I can't figure out. Here's what I've got so far: http://codepen.io/melissall/pen/PPWPQE I want the headline to be centered in the image until ...

  1. css javascript scroll to bottom
  2. css javascript scroll
  3. css js scroll
  4. css js scroll to top
  5. javascript css scroll position
  6. scrollbar css js
  7. css javascript animation scroll
  8. css javascript smooth scroll
  9. css js horizontal scroll
  10. css js smooth scroll
  11. css javascript disable scroll