LoginSignup
3
3

More than 5 years have passed since last update.

javascriptのオブジェクト

Posted at

オブジェクトの書き方

 var  hoge = {
    a: 123,
    b: 'abc',
    c: function(){
        return 'xyz';
    },
    d: [4,5,6]
 };

上記のオブジェクトhogeにはa, b, c,dの4つのプロパティがあり、aには123という整数、bには'abc'という文字列、cには文字列'xyz'を返す関数、dには配列[4,5,6]が定義されている。

 hoge.a = 789;

このように書き換えたり

 var l = hoge.b;

参照したり

 hoge.c();

関数を実行したり

 var m = hoge.d[0];

配列の中身を参照したりすることができる。

hoge.e = function(){
    return 'new';
}

hoge['f'] = function(){
    return 'new2';
}

また、オブジェクトに新しい関数等を追加することも可能 (キーを指定して追加も可)

3
3
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
3
3