2008/11/19(水)javascriptのpro...

と同じ罠にはまってしまった...
なにやってるんだか.

A = function(){}
A.prototype = {
  pos: {x:0, y:0}
}

a = new A();
b = new A();

a.pos.x = 10; a.pos.y = 20;
b.pos.x = 30; b.pos.y = 40;

a.pos.x; // -> 30;
a.pos.y; // -> 40;
b.pos.x; // -> 30;
b.pos.y; // -> 40;

正しくは↓の通り。

A = function(){
  this.pos = {x:0, y:0};
}
A.prototype = {
  pos: null
}

a = new A();
b = new A();

a.pos.x = 10; a.pos.y = 20;
b.pos.x = 30; b.pos.y = 40;

a.pos.x; // -> 10;
a.pos.y; // -> 20;
b.pos.x; // -> 30;
b.pos.y; // -> 40;

一度,以前作ったプログラムも見直した方がいいかもしれない