Search code examples
haxehaxeflixel

In a class difference between using `this.x` and just `x` to access a field?


In the haxe manual in the section about class instances they list the following code sample (simplified by me):

class Point {
  var x : Int;
  public function new(x) {
    this.x = x;
  }
}

In the section about class fields they list the following:

class Main {
  static var member:String = "bar";
  public static function main() {
    member = "foo";
  }
}

In the previous example they use this to access the x field, but in the next example they don't. Is this code equivalent or is there some nuance to it?


Solution

  • In first example in functions passed variable x, that have same name as class member. So this.x refers to class member. You could always use this to refer class members, but usually it omits if we not have such case as in first example, where we need explicitly refer to class member.