문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
|
wiki:javascript:javascript_note:js_classes_-_class_intro [2021/04/12 09:38] emblim98 만듦 |
wiki:javascript:javascript_note:js_classes_-_class_intro [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 42: | 줄 42: | ||
| =====Using a Class===== | =====Using a Class===== | ||
| 클래스가 있으면, 클래스를 사용하여 오브젝트를 만들 수 있습니다.\\ | 클래스가 있으면, 클래스를 사용하여 오브젝트를 만들 수 있습니다.\\ | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p id=" | ||
| + | < | ||
| + | class Car { | ||
| + | constructor(name, | ||
| + | this.name = name; | ||
| + | this.year = year; | ||
| + | } | ||
| + | } | ||
| + | myCar = new Car(" | ||
| + | document.getElementById(" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | \\ | ||
| + | 위의 예제는 **Car class**를 사용하여 두 개의 **Car objects**를 만듭니다.\\ | ||
| + | \\ | ||
| + | constructor 메서드는 새로운 오브젝트가가 생성될 때 자동으로 호출됩니다.\\ | ||
| + | |||
| + | =====The Constructor Method===== | ||
| + | constructor 메서드는 특별한 메서드입니다.\\ | ||
| + | * " | ||
| + | * 새로운 오브젝트가 생성되면 자동으로 실행됩니다. | ||
| + | * 오브젝트 속성(object properties)을 초기화하는 데 사용됩니다. | ||
| + | constructor 메서드를 정의하지 않으면, %%JavaScript%%는 빈(empty) constructor 메서드를 추가합니다.\\ | ||
| + | |||
| + | =====Class Methods===== | ||
| + | 클래스 메소드는 오브젝트 메소드와 동일한 구문으로 생성됩니다.\\ | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | 항상 '' | ||
| + | \\ | ||
| + | 그런 다음 원하는 수의 메서드를 추가합니다.\\ | ||
| + | ===Syntax=== | ||
| + | <code javascript> | ||
| + | class ClassName { | ||
| + | constructor() { ... } | ||
| + | method_1() { ... } | ||
| + | method_2() { ... } | ||
| + | method_3() { ... } | ||
| + | } | ||
| + | </ | ||
| + | \\ | ||
| + | Car age를 반환하는 " | ||
| + | ====예제==== | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p id=" | ||
| + | < | ||
| + | class Car { | ||
| + | constructor(name, | ||
| + | this.name = name; | ||
| + | this.year = year; | ||
| + | } | ||
| + | age() { | ||
| + | let date = new Date(); | ||
| + | return date.getFullYear() - this.year; | ||
| + | } | ||
| + | } | ||
| + | let myCar = new Car(" | ||
| + | document.getElementById(" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====예제==== | ||
| + | 클래스 메소드에 __매개 변수__(parameters)를 보낼 수 있습니다.\\ | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p id=" | ||
| + | < | ||
| + | class Car { | ||
| + | constructor(name, | ||
| + | this.name = name; | ||
| + | this.year = year; | ||
| + | } | ||
| + | age(x) { | ||
| + | return x - this.year; | ||
| + | } | ||
| + | } | ||
| + | let date = new Date(); | ||
| + | let year = date.getFullYear(); | ||
| + | let myCar = new Car(" | ||
| + | document.getElementById(" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | =====" | ||
| + | 클래스의 구문은 " | ||
| + | \\ | ||
| + | " | ||
| + | |||
| + | ====예제==== | ||
| + | "stict mode" | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p>In a JavaScript Class you cannot use variable without declaring it.</ | ||
| + | <p id=" | ||
| + | < | ||
| + | class Car { | ||
| + | constructor(name, | ||
| + | this.name = name; | ||
| + | this.year = year; | ||
| + | } | ||
| + | age() { | ||
| + | // date = new Date(); | ||
| + | let date = new Date(); // This will wrok | ||
| + | return date.getFullYear() - this.year; | ||
| + | } | ||
| + | } | ||
| + | myCar = new Car(" | ||
| + | document.getElementById(" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||