Water Sunlight

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

ユーザ用ツール

サイト用ツール


js:object:basic

差分

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

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

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
js:object:basic [2021/05/24 10:24]
y2sunlight [静的プロパティと静的メソッド]
js:object:basic [2021/07/19 10:58] (現在)
y2sunlight [静的プロパティと静的メソッド]
行 55: 行 55:
     suzuki.name = 'suzuki taro'; // プロパティの書き込み     suzuki.name = 'suzuki taro'; // プロパティの書き込み
     suzuki.greeting();           // メソッドの呼び出し     suzuki.greeting();           // メソッドの呼び出し
 +</sxh>
 +
 +=== メンバーの有無の判定(in演算子) ===
 +
 +in演算子を使って、メンバーの存在の有無を判定することができます。
 +
 +<sxh javascript;title:Example>
 +console.log('name' in suzuki);     // true
 +console.log('greeting' in suzuki); // true
 </sxh> </sxh>
  
行 94: 行 103:
 console.log(taro.getSchoolName()); // NG (taro.getSchoolName is not a function) console.log(taro.getSchoolName()); // NG (taro.getSchoolName is not a function)
 </sxh> </sxh>
-\\ 
  
 +=== メンバーの有無の判定(in演算子) ===
  
-===== prototypeオブジェクト =====+in演算子を使って、メンバーの存在の有無を判定することができます。
  
 <sxh javascript;title:Example> <sxh javascript;title:Example>
-var Student = function(name) { +console.log('name' in taro);             // true 
-    this.name = name+console.log('greeting' in taro)        // true 
-};+console.log('age' in taro)             // false 
 +console.log('getSchoolName' in taro);    // false
  
-Student.prototype.greeting = function() { +console.log('age' in Student);           // true 
-    console.log(`I am ${this.name}.`); +console.log('getSchoolNamein Student); // true 
-}; +</sxh>
- +
-// 年齢(age)はオブジェクト毎に保持されるべきなので、prototypeには適さない。 +
-// 但し参照のみの初期値として使用することはできる。 +
-Student.prototype.age = 15; +
- +
-// suzukiはStudentのprototypeの参照を保持する。  +
-var suzuki = new Student('suzuki'); +
-suzuki.greeting();    // 結果:I am suzuki. +
-</sxh>  +
- +
-=== プロトタイプを使用するメリット === +
- +
-  - メモリ量の節約できる +
-  - メンバの追加/変更が即座に反映される+
  
 \\ \\
行 172: 行 168:
 </sxh> </sxh>
 \\ \\
- 
-===== thisキーワード ===== 
- 
-^使用場所^参照先^ 
-|関数外|Globalオブジェクト| 
-|関数内|Globalオブジェクト(strictモードでundefined)| 
-|コンストラクタ|対象オブジェクト| 
-|メソッド|対象オブジェクト| 
-|イベントリスナー|対象オブジェクト| 
-|Functionオブジェクトの[[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/call|call]]メソッド|引数で指定されたオブジェクト| 
-|Functionオブジェクトの[[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/apply|apply]]メソッド|引数で指定されたオブジェクト| 
  
  
js/object/basic.1621819446.txt.gz · 最終更新: 2021/05/24 10:24 by y2sunlight