logo

JavaScript hasOwnProperty() 메서드

그만큼 hasOwnProperty() JavaScript의 메서드는 객체에 프로토타입 체인에서 상속되지 않고 객체에 직접 속하는 특정 속성이 있는지 확인하는 데 편리합니다. 객체가 자체 속성을 가지고 있는지 확인하는 데 유용한 도구입니다.

통사론:



object.hasOwnProperty( prop );>

매개변수 :

  • 소품: 테스트할 속성의 문자열 또는 기호 형식으로 이름을 보유합니다.

반환 값:

객체가 주어진 속성을 자체 속성으로 가지고 있는지 여부를 나타내는 부울 값을 반환합니다.



예시 1: 이 예에서는 개체의 속성을 확인합니다.

자바스크립트
function checkProperty() {  let exampleObj = {};  exampleObj.height = 100;  exampleObj.width = 100;  // Checking for existing property  result1 = exampleObj.hasOwnProperty('height');  // Checking for non-existing property  result2 = exampleObj.hasOwnProperty('breadth');  console.log(result1);  console.log(result2); } checkProperty()>

산출
true false>

예시 2: 이 예에서는 클래스 개체의 속성을 확인합니다.

자바스크립트
function checkProperty() {  function Car(a, b) {  this.model = a;  this.name = b;  }  let car1 = new Car('Mazda', 'Laputa');  // Checking for existing property  result1 = car1.hasOwnProperty('model');  // Checking for non-existing property  result2 = car1.hasOwnProperty('wheels');  console.log(result1);  console.log(result2); } checkProperty()>

산출
true false>

우리는 객체 메소드의 전체 목록과 이를 확인하기 위한 속성을 가지고 있습니다. JavaScript 객체 전체 참조 기사.



지원되는 브라우저:

  • Google 크롬 1 이상
  • 파이어폭스 1 이상
  • 인터넷 익스플로러 5.5 이상
  • 엣지 12 이상
  • 사파리 3 이상
  • 오페라 5 이상