[JavaScript] 객체의 특정 요소 소유 유무 확인 방법

Hyunwoo Seo·2023년 10월 23일
0

JavaScript

목록 보기
28/31
post-thumbnail

Object.hasOwnProperty


해당 메서드는 객체가 특정 프로퍼티를 소유했는지의 여부 를 true/false 로 반환한다.

특히 객체가 지정된 속성을 프로토타입 체인을 통해 상속되지 않은 그 객체의 직접 속성으로 포함하는지를 나타내는 boolean 을 반환한다.

const object_1 = {
	test_1:'test 1'
}

console.log( object_1.hasOwnProperty('test_1') ) // true
console.log( object_1.hasOwnProperty('test_2') ) // false

const object_2 = {
	test_1:'test 1',
    test_2:undefined
}

console.log( object_2.hasOwnProperty('test_1') ) // true
console.log( object_2.hasOwnProperty('test_2') ) // true

const object_3 = {
	test_1:'test 1'
}
Object_3.prototype.test_2 = undefined

console.log( object_3.hasOwnProperty('test_1') ) // true
console.log( object_3.hasOwnProperty('test_2') ) // false

0개의 댓글