Trouble with 'this.' keyword

0 favourites
  • 3 posts
From the Asset Store
Contains 20 loop-able music tracks for JRPG and adventure games!
  • I'm having some trouble getting this.something to reference what I want it to reference. I've read through this to help me:

    quirksmode.org/js/this.html

    My problem is that when I try to call this.runtime.trigger in certain locations in my code, it tells me that it can't call method trigger of undefined. So this.runtime obviously isn't pointing where I want it to point.

    Here's what I have:

    instanceProto.onCreate initiates a websocket. When that socket receives a message, I want it to store data from the message in an object that my conditions and expressions will be able to see. Originally I had this:

    instanceProto.onCreate = function() {

        //Other stuff

        var socket = new WebSocket(//My server's address

        //socket stuff

        socket.onmessage = function(evt) {

            this.data = evt.data;

        }

    }

    That wouldn't work, presumably because the 'this.' in 'this.data' refers to 'socket'. In order for my expressions to be able to reference the data as this.data, this would need to refer to... instanceProto? Say I want the following operation to work:

    cnds.thisIsACondition = function(indexOfWantedData) {

         if(this.data[indexOfWantedData] > 17) {

              return true;

         }

         return false;

    }

    I've gotten the conditions to read from the data object correctly using this setup:

    instanceProto.onCreate = function() {

        //Stuff

        socket.onmessage = function(evt) {

            instanceProto.updateData(evt.data);

        }

        //stuff

    }

    instanceProto.updateData = function(data) {

        this.data = data;

    }

    So this works. Marvelous! My conditions will read this correctly. Then I tried adding in the triggers, changing the last function to this:

    instanceProto.updateData = function(data) {

        this.data = data;

        this.runtime.trigger(cr.plugins_.MyPlugin.prototype.cnds.thisIsATrigger, this);

    }

    And I get errored out, with a "cannot call trigger of 'undefined'" thing.

    (I currently can't get the exact error message since I'm not on the computer I have the work on, but I can post the exact error message later today if necessary)

    So. If anyone knows why this.runtime isn't defined inside a function of instanceProto, despite the fact that this looks just like code I see in, for example, the Mouse plugin's onMouseDown trigger, I would love the help.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I think you have to use self instead of this

  • No, 'instanceProto' does not refer to your instance, it prefers to the instance's prototype. You can store data to it because javascript allows you to store data in any object, but it is a different object to your actual instance. It's sort of the object the instance 'inherits' methods from. More on prototypes - it's a really weird part of javascript, took me a while to get my head round it myself.

    I think in your first example 'this' is actually just the global scope, because

    socket.onmessage = function (e)
    {
        this.foo = 5;
    }

    is basically equivalent to

    function temp(e)
    {
        this.foo = 5;
    }
    socket.onmessage = temp;

    where it's more obvious that 'this' does not refer to anything. A good example of the solution is the Mouse object, which attaches a function to mousedown that uses a closure to permanently reference 'this' from the calling scope:

         jQuery(document).mousedown(
              (function (self) {
                   return function(info) {
                        // this is the real function - 
                        // use 'self' instead of 'this'
                        self.onMouseDown(info);
                   };
              })(this)
         );

    It's a bit fiddly syntax, but that is the correct solution. As you can see it's easiest just to call another function so you can use a real 'this' instead of keeping using 'self'.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)