0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

開眼!JavaScriptの2.8のまとめ for-inループでプロパティを列挙

Posted at

開眼!JavaScriptの2.8のまとめです。

##2.8 for-inループを使って、プロパティを列挙する
for-inを使うことによって、オブジェクトのプロパティをひとつひとつ列挙できる。ただしfor-inではプロトタイプチェーンから継承したプロパティも列挙されるため、対象オブジェクト自身のプロパティのみを列挙したい場合は、hasOwnProperty()メソッドを使用する。

for-inループとhasOwnProperty()メソッドを使って、そのオブジェクト自身が保持しているプロパティを列挙します。

var cody = {
    age : 33,
    gender : 'male'
};
for (var key in cody) {
    if (cody.hasOwnProperty(key)) {
        console.log(key);
    }
}

結果
age
gender

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?