Water Sunlight

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

ユーザ用ツール

サイト用ツール


js:object:object

差分

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

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

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
js:object:object [2021/03/30 10:22]
tanaka
js:object:object [2022/01/27 13:32] (現在)
tanaka [概要]
行 3: 行 3:
  --- //[[http://www.y2sunlight.com/water|y2sunlight]] 2021-03-30//  --- //[[http://www.y2sunlight.com/water|y2sunlight]] 2021-03-30//
  
-=== 概要 ===+===== 概要 =====
  
-全てのオブジェクトの共通的な性質機能を提供する+全てのオブジェクトの共通的な性質/機能を提供する
  
->TODO:+Objectメンバ(主要なもの) ※その他プロパティ/プロトタイプ/変更可否に関するものもある。 
 +|メンバ|概要| 
 +|constructor|インスタンス化で使用されたコンストラクタ| 
 +|toString()|インスタンスの文字列表現の取得| 
 +|toLocaleString()|インスタンスの文字列表現の取得(地域依存)| 
 +|valueOf()|インスタンスの基本型表現の取得(基本型を取得)| 
 +|create() ※|与えられたオブジェクトから新しいインスタンスを作る| 
 +|assign() <sup>[[js:top#ECMAScript|*2015]]</sup> ※|与えられたオブジェクトのコピー| 
 +|is() <sup>[[js:top#ECMAScript|*2015]]</sup> ※|与えられたオブジェクトの比較| 
 +※ 静的メソッド 
 +\\ \\
  
-Objectンバ(主要の) ※プロパティ/プロトタイプ/変更可否関するのもあ+===== create()ソッド ===== 
 + 
 +<code javascript> 
 +Object.create(proto, props);  // 新しいオブジェクトを作る 
 +</code> 
 + 
 +=== いろいろオブジェクト作り方 === 
 +<sxh javascript;title:Example> 
 +// 方法1 
 +var obj = {name:'taro', age:17}; 
 + 
 +// 方法2 
 +var obj = new Object()
 +obj.name = 'taro'; 
 +obj.age = 17; 
 + 
 +// 方法3 
 +var obj = Object.create(Object.prototype,
 +    name: {value:'taro', writable:true, configurable:true, enumerable:true}, 
 +    age: {value:17, writable:true, configurable:true, enumerable:true} 
 +}); 
 +</sxh> 
 +詳しくは[[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/create|MDN]]参照 
 + 
 +\\ 
 + 
 +===== assign()メソッド ===== 
 + 
 +<code javascript> 
 +Object.assign(target, source1, …);  // オブジェクトマージ 
 +</code> 
 + 
 +<sxh javascript;title:Example> 
 +Student1 = {name:'taro', age:17}; 
 +Student2 = {name:'hanako', tel:'090-1234-56789'}; 
 +Object.assign(Student1,Student2); 
 +console.log(Student1);  // 結果 {name:'hanako', age:17, tel:'090-1234-56789'
 +console.log(Student2);  // 結果 {name:'hanako', tel:'090-1234-56789'
 +             
 +// マージせずに新しいオブジェクトを作る時 
 +Object.assign({}, Student1);    // taroのみコピー 
 +Object.assign({}, Student1, Student1, Student2);    // taroをコピーしてhanakoをマージ 
 +</sxh> 
 +\\ 
 + 
 +===== 不変オブジェクト ===== 
 + 
 +  * インスタンス生成後、一切の状態を変更できないオブジェクト 
 +  * JavaScriptではObjectオブジェクトの静的メソッドを使用して不変オブジェクトを定義できる 
 + 
 +《変更可否を操作する静的メソッド》 
 +| |設定|確認| 
 +|プロパティの追加禁止|preventExtensions(obj);|isExtensible(obj)| 
 +|プロパティの追加/削除禁止|seal(obj)|isSealed(obj)| 
 +|ロパティの追加/削除/変更禁止 \\ (不変オブジェクト)|freeze(obj)|isFrozen(obj)| 
 +※Strictモードを有効しないと変更しようとして例外発生しない。有効にす事を推奨
  
js/object/object.1617067367.txt.gz · 最終更新: 2021/03/30 10:22 by tanaka