0 Favourites

this.lastData, theInstance.lastData and self.lastData

  • Studying the AJAX plugin as refence, i found "lastData" in 3 situations:

    Code 1

    pluginProto.Instance = function(type)
    	{	
           ...
    		this.lastData = "";[/code:3l7lvv5y]
    
    Code 2
    [code:3l7lvv5y]
    var theInstance = null;
    
    window["C2_AJAX_DCSide"] = function (event_, tag_, param_)
    	{
             ...
    			theInstance.lastData = param_;
    	}
    
    instanceProto.onCreate = function()
    	{
    		theInstance = this;
    	};
    [/code:3l7lvv5y]
    
    Code 3
    [code:3l7lvv5y]
    var self = this;
    
    var errorFunc = function ()
    		{
        self.lastData = data.replace(/\r\n/g, "\n")[/code:3l7lvv5y]
    
    Code 4
    [code:3l7lvv5y]Exps.prototype.LastData = function (ret)
    	{
    		ret.set_string(this.lastData);
    	};[/code:3l7lvv5y]
    
    The first 2 codes I think it´s talking about the "Instances" propertie (between the 3 Classes: Plugin, type and Intance).
    
    Why in code 3 is using "self" ?
    
    In code 4 the "this.lastData" refers to the instace too?
  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • In 1 and 4 "this" is the current instance.

    In 2 "this" in the onCreate function is the current instance. It's set to a global variable so the instance can be referenced in that widow callback.

    In 3 self is used so the callback "errorFunc" can access the instance. Otherwise I think this would be the global window object.

    The following link probably is better help:

    http://javascriptissexy.com/understand- ... master-it/

  • Thanks R0J0hound, just perfect!

    The problem was the nested function.

    When "this" is inside a nested expression function (example called "A"), that is inside the "instanceProto" function, Calling the "A" function will get you the global object, in Strict mode, undefined (that was the trick part). That´s the reason for defining the "self"...you are right.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Topic
0 Favourites
AJAX.Lastdata Problem
15 4,394
JoshTzx's avatar
JoshTzx
Topic
0 Favourites
AJAX.LastData
7 672
pognoss's avatar
pognoss
Topic
7 405
mindfaQ's avatar
mindfaQ