0

I'm new to classes in Javascript. I worked alot in C# but in JS classes are a bit different.

I want to fill a member variable in a class from a Ajax request but the value never changes. I think some scope issues?

Here is an example.

class bla {
    this.test = []; // JS Error
    var test2 = []; //JS Error

    constructor(blubb) {
        this.blubb = blubb; // Works
        this.test3 = []; //Works
    }

    load(){
        $.ajax({
            type: 'POST',
            url: ajaxURL,
            success: function(result) {
                this.test3 = result; // Doesn't work test3 is still empty
            }
        });
    }
}
Yunus K
  • 15
  • 5
  • 2
    Every `function` binds its own `this`, so you are currently not assigning the value to your object but to the context of the success function. – Teemoh Jul 25 '18 at 20:05
  • 1
    see: https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives – Odyssee Jul 25 '18 at 20:11

0 Answers0