2013年1月29日火曜日

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

  1. var a = [ 0, 1, 2 ];  
  2. alert( a.length ); // 3  
  3.   
  4. a[ 3 ] = 3;  
  5. alert( a.length ); // 4  
  1. var a = 3;  
  2. var b = a;  
  3. alert( "a = " + a ); // a = 3  
  4. alert( "b = " + b ); // b = 3  
  5.   
  6. a = 5;  
  7. alert( "a = " + a ); // a = 5  
  8. alert( "b = " + b ); // b = 3  
  1. var a = new Object();  
  2. a.property = 10;  
  3.   
  4. var b = new Object();  
  5. b = a;  
  6.   
  7. alert( "a.property = " + a.property ); // a.property = 10  
  8. alert( "b.property = " + b.property ); // b.property = 10  
  9.   
  10. a.property = 5;  
  11. alert( "a.property = " + a.property ); // a.property = 5  
  12. alert( "b.property = " + b.property ); // b.property = 5 !!!  
  13.   
  14. b.property = 8;  
  15. alert( "a.property = " + a.property ); // a.property = 8 !!!  
  16. alert( "b.property = " + b.property ); // b.property = 8  
  1. var a = { property : 10 };  
  2. var b = a;  
  3. alert( "a.property = " + a.property ); // a.property = 10  
  4. alert( "b.property = " + b.property ); // b.property = 10  
  5.   
  6. a.property = 5;  
  7. alert( "a.property = " + a.property ); // a.property = 5  
  8. alert( "b.property = " + b.property ); // b.property = 5 !!!  
  9.   
  10. b.property = 8;  
  11. alert( "a.property = " + a.property ); // a.property = 8 !!!  
  12. alert( "b.property = " + b.property ); // b.property = 8  
  • オブジェクト : 変数に代入したとき、その実体ではなく参照が代入される
  • プリミティブ : 実体がそのまま代入される

0 件のコメント:

コメントを投稿