LoginSignup
0
2

More than 5 years have passed since last update.

Javascript オブジェクトおさらい

Last updated at Posted at 2018-08-05

オブジェクトとは・・・名前と値のペアの集合です。
プロパティ・・・名前と値のペアのこと

オブジェクトリテラル式
{プロパティ名:プロパティ値、プロパティ名:プロパティ値、・・・}

オブジェクトリテラル 例

プロパティ名が識別子
{x:2,y:1}

文字列値
{"x":2,"y":1}・{'x':2,'y':1}

数値
{1:2,2:1}

様々な型
{x:2,y:1,enable:true,color:{r:255,g:255,b:255}}

代入式
var obj = {x:3,y:4};  オブジェクトを変数に代入

typeof obj; typeof演算子で型を判定するとobject

object

プロパティにアクセス

ドット演算子
print(obj.x) オブジェクトobjのプロパティxの値を表示

左に代入式を書くとプロパティに代入できる。
obj.x = 33;
print(obj.x);
33

ブラケット演算子[]

print(obj['x']); obj.xと同じ
3

var name = 'x';
print(obj[name]);

new式
オブジェクト生成のための式

newの後に書くのは関数名
(newに続けて関数名を書いた時、その関数をコンストラクタとして呼び出す)

var obj = new object();
typeof obj;
object

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2