Hi – I am trying to untangle my confusion in relation to my understanding of the ‘this’ keyword in an example illustrating how to invoke overridden methods.
Ok here goes:
[LIST=1][*]
As far as my understanding goes there is a rule in regards to when it occurs in functions, where ‘this’ refers to the global object
and another rule when it occurs within a function that happens to be a method, in which case this refers to the object which invokes that method
[COLOR=”Red”]If that is the case then I would be grateful if someone can explain the following context in which they occur and are there additional rule(s) linked to them, because in the code below they occur in a function literal which would lead me to assume that ‘this’ refers to the ‘global object’ presumably but that clearly does not make sense in this case :
[CODE]PositionedRectangle.prototype.toString = function( ) {
return “(” + [COLOR=”DarkGreen”]this[/COLOR].x + “,” + [COLOR=”#006400″]this[/COLOR].y + “) ” +
[COLOR=”Purple”]this[/COLOR].superclass.prototype.toString.apply([COLOR=”Sienna”]this[/COLOR]);
}
[COLOR=”Red”]Another words can someone kindly explain to me what the two occurrences of ‘this’ mean and which rules apply to them, in above code sample.