Jump to content

for in loops - passing the key as argument


joerrey
 Share

Recommended Posts

Hey, I'm wondering why the key of the loop will be passed to the closure as a text object: this.skillSelection[key]

 

The first log provides the expected string output of "key", e.g.: "Bash".

But the log within the closure provides the this.skillSelection[key] object.

        //skillSelection        this.skillSelection = {};        this.skillSelectionDistance = 20;        for (var key in this.player.skills){            this.skillSelection[key] = game.add.text(this.skillSelectionDistance, 450, this.player.skills[key].name, { font: '16px Arial', fill: "#000000" });            this.skillSelection[key].inputEnabled = true;            console.log(key) // "Bash" output            this.skillSelection[key].events.onInputUp.add(function(key){                console.log(key) // text object ouput            }, this);            this.skillSelectionDistance += 60;        }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...