LoginSignup
31
12

More than 3 years have passed since last update.

ESLintのバージョンあげたらhasOwnPropertyで怒られるようになった。

Last updated at Posted at 2019-07-01

ESLintのバージョンを6にあげたらいろいろな箇所でエラーになるように...

object.hasOwnProperty(key)で怒られてました。

検索すると説明がありました。

ESLint v6.0.0 の変更点まとめ

no-prototype-builtins」でひっかかってたんですね。

Examples of incorrect code for this rule: ダメな書き方

判定するときには下のようにしてたんですが、それでもダメなんですかねぇ?

if (obj && obj.hasOwnProperty(key)) {
 // something
}

Examples of correct code for this rule: 良い書き方

ESLintのサイトに説明があったのでこのように変更しました。

if (obj && Object.prototype.hasOwnProperty.call(obj, "key")) {
 // something
}

長い...

関数にしたほうがいいかも?

const hasProperty = (obj,key) => {
  return !!(obj) && Object.prototype.hasOwnProperty.call(obj, key);
}

みたいな?

31
12
5

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
31
12