Water Sunlight

軽量プログラミングの文法 - JavaScript/Python

ユーザ用ツール

サイト用ツール


js:object:chain

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

次のリビジョン
前のリビジョン
js:object:chain [2021/05/12 09:38]
tanaka 作成
js:object:chain [2021/05/24 12:56] (現在)
tanaka
行 2: 行 2:
  
  --- //[[http://www.y2sunlight.com/water|y2sunlight]] 2021-05-12//  --- //[[http://www.y2sunlight.com/water|y2sunlight]] 2021-05-12//
 +
 +===== prototypeオブジェクト =====
 +
 +<sxh javascript;title:Example>
 +var Student = function(name) {
 +    this.name = name;
 +};
 +
 +Student.prototype.greeting = function() {
 +    console.log(`I am ${this.name}.`);
 +};
 +
 +// 年齢(age)はオブジェクト毎に保持されるべきなので、prototypeには適さない。
 +// 但し参照のみの初期値として使用することはできる。
 +Student.prototype.age = 15;
 +
 +// suzukiはStudentのprototypeの参照を保持する。 
 +var suzuki = new Student('suzuki');
 +suzuki.greeting();    // 結果:I am suzuki.
 +</sxh> 
 +
 +=== プロトタイプを使用するメリット ===
 +
 +  - メモリ量の節約できる
 +  - メンバの追加/変更が即座に反映される
 +
 +\\
  
 ===== プロトタイプチェーン ===== ===== プロトタイプチェーン =====
行 43: 行 70:
  
 var taro = new Dog('Taro'); var taro = new Dog('Taro');
-taro.cry();  // Taro cry: 'woooo'.+taro.cry();  // 結果:Taro cry: 'woooo'.
 </sxh> </sxh>
 \\ \\
行 69: 行 96:
 </sxh> </sxh>
  
-constructorプロパティは継承元を返す+constructorプロパティは継承元を返す
  
 \\ \\
  
-===== constructorプロパティとinstanceof =====+===== isPrototypeOfメソッド =====
  
 <sxh javascript;title:Example> <sxh javascript;title:Example>
行 90: 行 117:
 </sxh> </sxh>
  
-obj.prototype.isPrototypeOf(a)はobjプロトタイプがaのプロトタイプに存在するか否かを返す+obj.prototype.isPrototypeOf(a)はobjプロトタイプがaのプロトタイプに存在するか否かを返す
  
 \\ \\
js/object/chain.1620779931.txt.gz · 最終更新: 2021/05/12 09:38 by tanaka