foldrr's weblog

旧ブログ http://d.hatena.ne.jp/foldrr/

js で自クラス内で自分のインスタンスが作れない?

JmEditor の DMonkey では自クラス内で自分のインスタンスが作れない?
以下のコードで何故か ENameError が発生する。
なぜに…。

class Point {
  function Point(x, y){
    this.x = x;
    this.y = y;
  }
  
  function to(x, y){
    return new Line(this, new Point(x, y));
  }
}
class Line {
  function Line(start, end){
    this.start = start;
    this.end = end;
  }
}

point1 = new Point(100, 100);
line1 = point1.to(200, 200);
line1.start.x;  //ENameError が起きない。
line1.end.x;    //ENameError が起る。


どうしても分からないので 2ch に投げてみた。
http://pc11.2ch.net/test/read.cgi/software/1187267009/109-
バグらしい。
ひとまず↓で回避できる。

function Point(x, y){
  this.x = x;
  this.y = y;
  
  this.to = function(x, y){
    return new Line(this, new Point(x, y));
  };
}

function Line(start, end){
  this.start = start;
  this.end = end;
}

point1 = new Point(100, 100);
line1 = point1.to(200, 200);
line1.start.x;  //ENameError が起きない。
line1.end.x;    //ENameError が起る。