オブジェクトリテラル
new Object【コンストラクタ】
create【作成】
オブジェクトリテラル
new Object【コンストラクタ】
メモ
概要
- Object【オブジェクト】オブジェクトの生成
- new により生成
- オブジェクトリテラルにより生成
関連
外部リンク
- ECMA-262 (英語)
Object ( [ value ] ) ES2024 (15) ES2023 (14) ES2022 (13) Property Accessors ES2024 (15) ES2023 (14) ES2022 (13)
構文
new Object( [value] )
(Object) オブジェクト
value 値 (詳細は下記参照)
※:オブジェクトリテラルについては、オブジェクトリテラル 参照
値 | 作成オブジェクト |
---|---|
省略 null undefined | 空のオブジェクト |
文字列 | String【文字列】オブジェクト |
Boolean値 | Boolean【真偽値】オブジェクト |
数値 | Number【数値】オブジェクト |
例
console.log(new Object());
// 出力:Object { }
console.log(new Object(null));
// 出力:Object { }
console.log(new Object(undefined));
// 出力:Object { }
console.log(new Object('string'));
// 出力:String { "string" }
console.log(new Object(true));
// 出力:Boolean { true }
console.log(new Object(123));
// 出力:Number { 123 }
// プロパティ アクセス
const point = new Object({ x: 10, y: 20 });
console.log(point.x, point['y']);
// 出力:10 20
point.x = 100;
point['y'] = 200;
console.log(point.x, point['y']);
// 出力:100 200
// オブジェクトリテラル
let undef;
const num = 1234;
const str = 'string';
const bool = true;
const point = { x: 111, y: 222 };
const strNum = 'number-';
const obj = {
undef,
num,
str,
bool,
point,
x:10,
"y": 20,
123: 123456,
['z']: 30,
[strNum + '567']: 567,
// 関数定義
log1 : function ( propName ) { console.log(this[propName]); },
// 関数定義の省略形
log2 ( propName ) { console.log(this[propName]); },
// ゲッター
get xValue () { return this.x; },
// セッター
set xValue ( value ) { this.x = value; },
}
console.dir(obj);
// 出力例:
// Object { ~ }
// 123: 123456
// bool: true
// log1: function log1(propName)
// log2: function log2(propName)
// num: 1234
// "number-567": 567
// point: Object { x: 111, y: 222 }
// str: "string"
// undef: undefined
// x: 10
// xValue: (...)
// get xValue: xValue()
// set xValue: xValue( value )
// y: 20
// z: 30
debugger;
// 書き込み
obj['x'] = 100;
obj.y = 200;
obj['z'] = 300;
obj[strNum + '567'] = 56789;
obj['123'] = 321;
obj.num = 4321;
obj.str = "STRING";
obj.bool = false;
obj.point.x = 333;
obj.point.y = 444;
console.dir(obj);
// 出力例:
// Object { ~ }
// 123: 321
// bool: false
// log1: function log1(propName)
// log2: function log2(propName)
// num: 4321
// "number-567": 56789
// point: Object { x: 333, y: 444 }
// str: "STRING"
// undef: undefined
// x: 1000
// xValue: (...)
// get xValue: xValue()
// set xValue: xValue( value )
// y: 200
// z: 300
// セッター
obj.xValue = 1000;
// ゲッター
console.log( obj.xValue );
// 出力:1000
// 関数
obj.log1( 'x' );
// 出力:1000
obj.log1( 'y' );
// 出力:200
obj.log2( 'x' );
// 出力:1000
obj.log2( 'y' );
// 出力:200
Object.create【作成】
メモ
概要
- オブジェクトを作成
関連
外部リンク
- ECMA-262 (英語)
Object.create ( O, Properties ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Object.create( O [, Properties] )
オブジェクト
O オブジェクト
Properties プロパティ (プロパティ名 + 属性:詳細は下記参照)
TypeError 例外 O (オブジェクト)がオブジェクト・null 以外
Properties (プロパティ)の属性
属性 | デフォルト値 |
---|---|
value | undefined |
get | undefined |
set | undefined |
writable | false |
enumerable | false |
configurable | false |
例
const prop = { x: { value:0, writable:true }, y: { value:0 } };
const point = Object.create(Object.prototype, prop);
console.log(point);
// 出力:Object {x: 0, y: 0}
point.x = 10;
point.y = 10;
console.log(point);
// 出力:Object {x: 10, y: 0}
// const ng = Object.create(123);
// TypeError: 123 is not an object or null