다음을 통해 공유


in 연산자

업데이트: 2007년 11월

개체에 속성이 존재하는지 여부를 테스트합니다.

property in object

인수

  • property
    필수적 요소. 문자열로 계산되는 식입니다.

  • object
    필수적 요소. 임의의 개체입니다.

설명

in 연산자는 개체에 property라는 속성이 있는지 확인합니다. 또한 개체의 프로토타입을 확인하여 property가 프로토타입 체인의 일부인지 검사합니다. property가 개체나 프로토타입 체인에 있으면 in 연산자는 true를 반환하고 그렇지 않으면 false를 반환합니다.

in 연산자는 for...in 문과 혼동하지 않아야 합니다.

참고:

개체 자체에 속성이 있는지 여부와 프로토타입 체인에서 속성을 상속하지 않는지 테스트하려면 개체의 hasOwnProperty 메서드를 사용합니다.

예제

다음 예제에서는 in 연산자의 사용 예를 보여 줍니다.

function cityName(key : String, cities : Object) : String {
   // Returns a city name associated with an index letter.
   var ret : String = "Key '" + key + "'";
   if( key in cities )
      return ret + " represents " + cities[key] + ".";
   else  // no city indexed by the key
      return ret + " does not represent a city."
}

// Make an object with city names and an index letter.
var cities : Object = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"}

// Look up cities with an index letter.
print(cityName("a",cities));
print(cityName("z",cities));

이 코드는 다음과 같이 출력됩니다.

Key 'a' represents Athens.
Key 'z' does not represent a city.

요구 사항

버전 1

참고 항목

개념

연산자 우선 순위

연산자 개요

참조

for...in 문

hasOwnProperty 메서드