Sign in to follow this  
A.Russell

Typescript Variable Scope

Recommended Posts

Anther WTF moment with Typescript. It is similar to a previous problem I had, but doesn't have the same solution. Could someone explain this, please? 

module TestGame{export class StateSettingStage extends Phaser.State {  sally: TargetSprite; //subclass of Phaser.Sprite  shoes: Token; //subclass of Phaser.Sprite  this.sally = new TargetSprite(this.game, 350, 20, "sally");  this.game.add.existing(this.sally);  this.shoes= new TargetSprite(this.game, 350, 20, "shoes");  this.game.add.existing(this.shoes);  ->this.shoes.setTargetSprite(this.sally); <--- No problem  this.clothes.children.forEach(function (val: Token) {                val.anchor.setTo(0.5, 0.5);                //val.setTargetSprite(this.sally); <-- this is what I really wanted to do, but...->              this.shoes.setTargetSprite(this.sally); <-- this.shoes and this.marry are undefined!            })  }}

Why are "shoes" and "sally" undefined when within the foreach callback function? I checked, and there is no argument I can use to pass these variables to the callback function (not that I should need to anyway, because they are within scope!)

Share this post


Link to post
Share on other sites

Good ole 'this' identity crisis...

Inside your forEach callback, 'this' is not what it was while outside your callback...

 

Is 'this.clothes' a Phaser.Group...? You could remove the 'children' part of that line and call forEach on the group instead, and pass 'this' in as the callback context, and then 'this' inside the callback will be the same 'this' as the 'this' outside the callback...

this.clothes.forEach(function (val: Token) {                val.anchor.setTo(0.5, 0.5);                //val.setTargetSprite(this.sally); <-- this is what I really wanted to do, but...->              this.shoes.setTargetSprite(this.sally); <-- this.shoes and this.marry are undefined!            }, this);

(I love talking about 'this' in javascript, it sounds so confusing but makes perfect sense...! kinda.....)

Share this post


Link to post
Share on other sites

I get the idea, but I don't know the syntax to pull it off.

 

Simply adding => void, or :void => was a syntax error. 

 

So, I started to try this:

 

//this function is correct syntax using the lambda:

var interateChildren = (child: Token): void => {
                child.anchor.setTo(0.5, 0.5);
                child.setTargetSprite(this.sally);

            };. 

 

But that won't fit with: 

 

this.clothes.children.forEach(function(val: Token): void  {
                
            }
 
...and it's just a big mess, because I don't really understand it.  
 
What is the correct syntax?

Share this post


Link to post
Share on other sites
        private collection: string[] = [];        private apple: string = "apple";        constructor() {            this.collection.push("hi");            this.collection.forEach((value: string, index: number, array: string[]): void => {                console.log(this);  //this.apple is fine, without the lambda, this would mean this function scope.... has no "apple"                console.log(value, index, array)            });        }
 
Outputs
 
this: Game  (The above was a Game class)
value: "hi"
index: 0
array: ["hi"];

 

Share this post


Link to post
Share on other sites

XeDeath, thanks again. That was a copy-and-paste-perfect solution. I really have trouble getting my head around "this" not being "this." I am going to have to look into "this" more. "This" is giving me a headache. 

 

Clark: I got a syntax error when I tried to use the lambda. At a glance the only difference I can see is that you have used all of foreach's arguments. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.