# 생성자와 new

## 객체

**객체**란 서로 **연관된 변수와 함수를 그룹핑한 그릇**이라고 할 수 있다. **객체 내의 변수를 프로퍼티(property) 함수를 메소드(method)**&#xB77C;고 부른다.

```javascript
var person = {} // 비어있는 객체 
person.name = 'egoing'; // 프로퍼티
person.introduce = function(){//메소드
    return 'My name is '+this.name; 
}
document.write(person.introduce()); // My name is egoing 

// 좀더 나은 방법 
var person = {
    'name' : 'egoing', // 프로퍼티
    'introduce' : function(){ //메소드
        return 'My name is '+this.name;
    }
}
document.write(person.introduce()); // My name is egoing 
```

###

### 생성자

**생성자(constructor)는 객체를 만드는 역할을 하는 함수다.** 자바스크립트에서 함수는 재사용 가능한 로직의 묶음이 아니라 **객체를 만드는 창조자**라고 할 수 있다.

```javascript
function Person(){}
var p = new Person(); // new 아주 중요함 -> new 가 붙어있는 함수를 객체의 생성자라고 한다. 
// 함수에 new를 붙이면 그것은 객체가 된다. 
p.name = 'egoing';
p.introduce = function(){
    return 'My name is '+this.name; 
}
document.write(p.introduce()); // My name is egoing
```

```javascript
function Person(name){ // 생성자 생성 
    this.name = name; // eging
    this.introduce = function(){
        return 'My name is '+this.name; 
    }   
}
var p1 = new Person('egoing');
document.write(p1.introduce()+"<br />"); //  My name is egoing
 
var p2 = new Person('leezche');
document.write(p2.introduce());  //  My name is leezche
```

**생성자 내에서 이 객체의 프로퍼티를 정의**하고 있다. **이러한 작업을 초기화**라고 한다. 이를 통해서 코드의 **재사용성이 대폭 높아졌다.**

코드를 통해서 알 수 있듯이 **생성자 함수**는 **일반함수와 구분하기 위해서 첫글자를 대문자**로 표시한다.


---

# Agent Instructions: 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://leeboa.gitbook.io/study/undefined-13/undefined-11/new.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.
