in 연산자는 명시된 속성이 명시된 객체에 존재하면 true를 반환합니다.

Link to section구문

속성 in 객체명

Link to section인자

속성
속성의 이름이나 배열의 인덱스를 뜻하는 문자열 또는 수 값입니다.
객체명
객체의 이름입니다.

Link to section설명

 다음 예제들은 in 연산자의 용도를 보여 줍니다.

// 배열
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees         // true를 반환합니다.
3 in trees         // true를 반환합니다.
(1 + 2) in trees   // true를 반환합니다. 연산자 우선 순위에 의하여 이 구문의 괄호는 없어도 됩니다.
6 in trees         // false를 반환합니다.
"bay" in trees     // false를 반환합니다. 당신은 배열의 내용이 아닌, 인덱스 값을 명시하여야 합니다.
"length" in trees  // true를 반환합니다. length는 Array(배열) 객체의 속성입니다.

// 미리 정의된 객체
"PI" in Math       // true를 반환합니다.
"P" + "I" in Math  // true를 반환합니다.

// 사용자가 정의한 객체
var myCar = {company: "Lamborghini", model: "Lamborghini Veneno Roadster", year: 2014};
"company" in myCar // true를 반환합니다.
"model" in myCar   // true를 반환합니다.

 당신은 반드시 in 연산자의 오른쪽에 객체를 명시하여야 합니다. 예컨대 당신은 String 생성자로 만들어진 문자열을 명시할 수 있지만 문자열 리터럴은 명시할 수 없습니다.

var color1 = new String("green");
"length" in color1 // true를 반환합니다. string 객체는 length, anchor, big등의 프로퍼티를 가지고 있기때문에 true

var color2 = "coral";
"length" in color2 // color2는 String 객체가 아니리 원시자료형이기 때문에 오류를 냅니다.

Link to section제거되었거나 정의되지 않은 속성에 대하여 in 연산자 사용하기

 in 연산자는 delete 연산자로 제거된 속성에 대하여 false를 반환합니다.

var myCar = {company: "Lamborghini", model: "Lamborghini Veneno Roadster", year: 2014};
delete myCar.company;
"company" in myCar; // false를 반환합니다.

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
3 in trees; // false를 반환합니다.

 만약 당신이 속성을 undefined로 설정하였는데 그것을 제거하지 않으면, in 연산자는 그 속성에 대하여 true를 반환합니다.

var myCar = {company: "Lamborghini", model: "Lamborghini Veneno Roadster", year: 2014};
myCar.company = undefined;
"company" in myCar; // true를 반환합니다.
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
3 in trees; // true를 반환합니다.

Link to section상속된 속성

 in 연산자는 프로토타입 체인에 의하여 접근할 수 있는 속성에 대하여 true를 반환합니다.

"toString" in {}; // true를 반환합니다.

Link to section명세

명세StatusComment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Relational Operators' in that specification.
Draft 
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Relational Operators' in that specification.
Standard 
ECMAScript 5.1 (ECMA-262)
The definition of 'The in Operator' in that specification.
Standard 
ECMAScript 3rd Edition (ECMA-262)
The definition of 'The in Operator' in that specification.
Standard초기의 정의가 담겨 있습니다. JavaScript 1.4에 추가되었습니다.

Link to section브라우저 호환성

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

기능ChromeFirefox (Gecko)Internet ExplorerOperaSafari
지원(Yes)(Yes)(Yes)(Yes)(Yes)

Link to section관련 문서


+ Recent posts