> For the complete documentation index, see [llms.txt](https://seula00027.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://seula00027.gitbook.io/til/javascript/object-object.md).

# Object 객체

* 연관있는 변수와 함수를 묶어준다.
* prototype을 이용해 상속이 가능하다.&#x20;
  * 이전 Ultra -> Super -> Sub 를 상속받던 예시에서 사실 Ultra는 Object를 상속받고 있다.
  * Object -> Ultra -> Super -> Sub&#x20;
* Object의 prototype은 모든객체의 prototype이다. 모든객체의 원형이다.

## Object의 생성자의 메소드와 Object의 프로토타입 객체

```javascript
// Object.keys() 생성자 메소드
var arr = ['a','b','c'];
console.log(Object.keys(arr));

//Object.prototype.toString() 프로토타입객체 
var a = new Array(1,2,3);
console.log(a.toSring());
```

1번과 2번의 사용방법이 왜 다를까? 1번 Object.keys()는 인자를 받아서 사용한다.

```javascript
Object.key(arr) //의 Object는 생성자 함수일 것이고
Object.keys = function(){}; // 이렇게 정의되어 있을 것이다.
```

2번 Object.prototype.toString()은

```javascript
Object.prototype.toSring = function(){};
```

이렇게 prototype으로 정의되어 있을 것이다.

메소드가 프로토타입이라는 의미는 new Object();라고 실행을 하는 순간에 어떠한 객체를 만들고 Object.prototype 이라는 특수한 프로퍼티의 저장되어 있는 객체를 원형으로 하는 객체가 생성된다. 그렇게 생성된 객체는 toString이라는 메소드를 쓸 수 있다.

> 이 두 차이를 이해해야 한다.

## Object 객체 확장

```javascript
Object.prototype.contain = function(needle){
    for(var name in this){
        if(this[name] === needle){
            return true;
        }
    }
    return false;
}

var o = {name: 'sla', 'city':'seoul'};
console.log(o.contain('sla'));
```

## Object 확장의 위험

```javascript
Object.prototype.contain = function(needle){
    for(var name in this){
        if(this[name] === needle){
            return true;
        }
    }
    return false;
}

var o = {name: 'sla', 'city':'seoul'};
//console.log(o.contain('sla'));

for(var name in o){
    console.log(o[name]);
} 

//결과는
/*
sla
seoul
ƒ (needle){
    for(var name in this){
        if(this[name] === needle){
            return true;
        }
    }
    return false;
}
*/
```

이렇게 contain 까지 같이 나온다. 모든 객체에 영향을 준거다.

```javascript
Object.prototype.contain = function(needle){
    for(var name in this){
        if(this[name] === needle){
            return true;
        }
    }
    return false;
}

var o = {name: 'sla', 'city':'seoul'};
//console.log(o.contain('sla'));

for(var name in o){
    console.log(name);
} 
//결과
//name
//city
//contain
```

contain 이 포함되어 있다.

> object에 프로퍼티에 추가한다는건 편리하면서도 위험하다.

## 그래도 써야한다면 hasOwnProperty

```javascript
Object.prototype.contain = function(needle){
    for(var name in this){
        if(this[name] === needle){
            return true;
        }
    }
    return false;
}

var o = {name: 'sla', 'city':'seoul'};
//console.log(o.contain('sla'));

for(var name in o){
    if(o.hasOwnProperty(name)){
        console.log(name);    
    }
}
```

자신의 property인지 체크해준다. 부모로 부터 상속받은 것은 제외된다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://seula00027.gitbook.io/til/javascript/object-object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
