2013年1月29日火曜日

オブジェクト・プリミティブへの代入実験

 var a = [ 0, 1, 2 ];
 alert( a.length ); // 3

 a[ 3 ] = 3;
 alert( a.length ); // 4
 var a = 3;
 var b = a;
 alert( "a = " + a ); // a = 3
 alert( "b = " + b ); // b = 3

 a = 5;
 alert( "a = " + a ); // a = 5
 alert( "b = " + b ); // b = 3
 var a = new Object();
 a.property = 10;

 var b = new Object();
 b = a;

 alert( "a.property = " + a.property ); // a.property = 10
 alert( "b.property = " + b.property ); // b.property = 10

 a.property = 5;
 alert( "a.property = " + a.property ); // a.property = 5
 alert( "b.property = " + b.property ); // b.property = 5 !!!

 b.property = 8;
 alert( "a.property = " + a.property ); // a.property = 8 !!!
 alert( "b.property = " + b.property ); // b.property = 8
 var a = { property : 10 };
 var b = a;
 alert( "a.property = " + a.property ); // a.property = 10
 alert( "b.property = " + b.property ); // b.property = 10

 a.property = 5;
 alert( "a.property = " + a.property ); // a.property = 5
 alert( "b.property = " + b.property ); // b.property = 5 !!!

 b.property = 8;
 alert( "a.property = " + a.property ); // a.property = 8 !!!
 alert( "b.property = " + b.property ); // b.property = 8
  • オブジェクト : 変数に代入したとき、その実体ではなく参照が代入される
  • プリミティブ : 実体がそのまま代入される

0 件のコメント:

コメントを投稿