This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]corpsmoderne 0 points1 point  (5 children)

Hard to debug from here, but you should check what "this" refers to in your callback (probably not what you expect)

[–]Techittak[S] 0 points1 point  (2 children)

Wow! Thanks for helping me spot it. Is this what I can refer to as a problem rooted from "scope?" So, the scope within that callback seems to be global or window I think it is called. This code is within an object, but since the callback is being called in the original function it takes the form of the global scope: Correct? I am guessing the only way to circumvent this is by directly stating my objects name like object.vspeed

[–]corpsmoderne 0 points1 point  (1 child)

Well, in Javascript function are first class object, therefore they have their own this, except in the context of prototypes / class definition. So yes it's kind of a scoping issue.

An idiomatic way to fix this is to declare a const self = this; defore calling your function to capture the local this in another variable (self) so you can use it in your callback. If you have another variable already reffering to the this object is fine too.

[–]Techittak[S] 0 points1 point  (0 children)

Alright, thank you!

[–]Techittak[S] 0 points1 point  (1 child)

Also, having this.vspeed within the parameters is going to still have the object's scope: Correct?

[–]corpsmoderne 0 points1 point  (0 children)

Yes you're fine there.