0
0

More than 1 year has passed since last update.

for...in...は使わない

Posted at

個人的な考えです。
僕は、for...in...文を使わずに、for...of...文で代替しています。

const obj = { a:1,b:2 };

for(const [key,value] of Object.entries(obj)){
    
    console.log(key,value);

}

理由は二つあります。

① 単純に、for...in...文でobj[key]とするのが面倒だから。
② Object.hasOwnPropaty()を使用しないと、プロトタイプチェーン内も対象としてしまうから。

懸念点は、Object.entries(obj)によるパフォーマンス低下ですが、現時点では特に気にせずに使用しています。

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