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