- MDN : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn
vs hasOwnProperty
hasOwnProperty를 대체하기 위한 메서드다.object[property] !== undefined 를 해도 되지만, 이게 훨씬 직관적이다.Object.create(null)를 사용하여 생성된 객체와 상속된 hasOwnProperty() 메서드를 재정의한 객체에 대해서도 동작하기 때문에 Object.hasOwnProperty()보다 권장된다.
외부 객체에서
Object.prototype.hasOwnProperty()를 호출하여 이러한 문제를 해결할 수 있지만 Object.hasOwn()이 더 직관적입니다.파라미터
instance: 자바스크립트 오브젝트
prop: 있는지 확인하고 싶은 프로퍼티명(string)
예시
const obj = {text:"hello"}; Object.hasOwn(obj, 'text'); // true Object.hasOwn(obj, 'asd'); // false
반환값
boolean 값을 반환한다.
해당 프로퍼티가 있다면
true, 없다면 false.- 프로퍼티의 value 값이
null또는undefined일 때도false를 반환한다.