1

I'm getting used to JS still and I need a bit of help.

On the next class I want to call saveTiles inside loadMap since I want the lines array ( I can't return it, so I did the saveTiles() function ).

But when called I have a saveTiles is not defined error. I can't either use the prototype definition nor assign saveTiles to a var

What am I doing wrong? Is there a better thinking on how can I update the this.tiles property of Map ?

Thank you very much!

class Map{
    constructor(){
        this.xTiles = 32;
        this.yTiles = 19;
        this.tiles = new Array();
    }

    saveTiles(lines) {
        var xMap = new Array();
        var totalCont = 0;
        while(totalCont < lines.length){
            xMap.push(lines[totalCont++]);
            if(totalCont % this.xTiles == this.xTiles - 1){
                this.tiles.push(xMap);
                xMap = [];
            }
        }
    }

    loadMap(map){
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", map, false);
        rawFile.onreadystatechange = function (){
            if(rawFile.readyState === 4)
                if(rawFile.status === 200 || rawFile.status == 0){
                    var allText = rawFile.responseText;
                    var lines = allText.split(' ');
                    this.saveTiles(lines); // error -> saveTiles is not defined
                }
        };
        rawFile.send(null);
    }
}
  • 1
    Also `this` in the scope you're using is incorrect. It's the scope of the function, which doesn't have that function defined. – jdmdevdotnet Aug 14 '17 at 15:05
  • [OOP in JS](http://phrogz.net/JS/Classes/OOPinJS.html) – Cheloide Aug 14 '17 at 15:06
  • @jdmdevdotnet: *"It's the scope of the function"* No, it isn't. It's whatever `XMLHttpRequest` sets it to when calling the function (in the case of XHR, it refers to the XHR object). `this` and scope have very little to do with one-another in JavaScript. But `this` is indeed the problem, hence the dupetarget. – T.J. Crowder Aug 14 '17 at 15:06
  • 2
    @manutheking: See the linked question's answers for details. In your case in the above, just making your `onreadystatechange` callback an arrow function will fix it: `rawFile.onreadystatechange = () => { /* ... */ };` (but see the link for *why*). – T.J. Crowder Aug 14 '17 at 15:08
  • try Map.saveLines(). As the function invocation is under onreadystatechange function and this inside refers to window object.There is a violation of scope – AL-zami Aug 14 '17 at 15:09
  • @T.J.Crowder `this` would be the scope of the onstatereadychange function. I don't really understand what you're getting at. – jdmdevdotnet Aug 14 '17 at 15:09
  • @jdmdevdotnet: Again: `this` and scope have very little to do with one another in JavaScript. Saying `this` is "the scope of the function" is simply incorrect. Well-meaning, I'm sure, but incorrect. Scope and `this` are largely unrelated. Just about the only thing that links them is that `this` can never *change* during a specific execution of a scope. (It can be different in an inner or outer scope.) Other than that, they're unrelated concepts. – T.J. Crowder Aug 14 '17 at 15:12
  • 1
    @T.J.Crowder Thank you very much, the link explained it very well! – manutheking Aug 14 '17 at 15:14

0 Answers0