若手IT勉強会 - 第5回勉強会 サイ本読書会第9章

第5回勉強会 - 若手IT勉強会 - アットウィキ
↓を使ってコードの実行デモをやった。
Learning Advanced JavaScript
随時更新

9.2プロトタイプと継承

new演算子がやる事

  1. 空のオブジェクトを作成
  2. コンストラクタ関数のprototypeプロパティの値をオブジェクトのプロトタイプとして設定
  3. コンストラクタ関数を実行

9.2.1継承プロパティへのアクセス

読み書きで非対称になっている
読み込み

  1. オブジェクト自身のプロパティの存在チェック
  2. プロトタイプのプロパティの存在チェック

書き込み

  • 常にオブジェクト自身のプロパティに書き込み
function Hoge(x){
  this.x = x;     
}
Hoge.prototype = {getX : function(){return this.x;}
  ,y:4
};
var hoge = new Hoge(3);
hoge.y=3;
log(hoge.y);
log(Hoge.prototype.y);

9.5 スーパークラスとサブクラス

constructorプロパティの書き換えが必要な条件は?

9.7.1 instanceof演算子

プロトタイプチェーン上のすべてのコンストラクタでtrue

function Hoge(){
}
Hoge.prototype = new Date();
(new Hoge()) instanceof Hoge;//true
(new Hoge()) instanceof Date;//true
(new Hoge()) instanceof Object;//true

7.4.7 isPrototypeOfメソッド

Hoge.prototype.isPrototypeOf(new Hoge());//true
Date.prototype.isPrototypeOf(new Hoge());//true
Object.prototype.isPrototypeOf(new Hoge());//true