문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
|
wiki:javascript:javascript_note:js_object_constructors [2021/05/03 11:10] emblim98 만듦 |
wiki:javascript:javascript_note:js_object_constructors [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 11: | 줄 11: | ||
| [[https:// | [[https:// | ||
| \\ | \\ | ||
| + | <code javascript> | ||
| + | // Constructor function for Person Objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | } | ||
| + | // Create a Person object | ||
| + | let myFather = new Person(" | ||
| + | // Display age | ||
| + | document.getElementById(" | ||
| + | "My father is " + myFather.age + " | ||
| + | </ | ||
| + | \\ | ||
| + | 대문자 첫 번째 철자로 생성자 함수(constructor function)의 이름을 지정하는 것이 좋습니다. | ||
| + | \\ | ||
| + | =====Object Types (Blueprints) (Classes)===== | ||
| + | 이전 챕터(JS Object Constructors)의 예제들은 제한적입니다. 그 예제들은 하나의 오브젝트만 만듭니다.\\ | ||
| + | \\ | ||
| + | 때때로 우리는 동일한 " | ||
| + | \\ | ||
| + | " | ||
| + | \\ | ||
| + | 위의 예제에서 '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | <code javascript> | ||
| + | // Constructor function for Person objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | } | ||
| + | // Create two person objects | ||
| + | let myFather = new Person(" | ||
| + | let myMother = new Person(" | ||
| + | // Display age | ||
| + | document.getElementById(" | ||
| + | "My father is " + myFather.age + ". My mother is " + myMother.age + " | ||
| + | // My father is 50. My mother is 48. | ||
| + | </ | ||
| + | \\ | ||
| + | =====The this Keyword===== | ||
| + | %%JavaScript%%에서 '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | 생성자 함수에서, | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | =====Adding a Property to an Object===== | ||
| + | 기존의 오브젝트에 새로운 프로퍼티를 추가하는 것은 쉽습니다.\\ | ||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | // Constructor function for Person objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | } | ||
| + | // Create 2 Person objects | ||
| + | let myFather = new Person(" | ||
| + | let myMother = new Person(" | ||
| + | // Add nationality to first object | ||
| + | myFather.nationality = " | ||
| + | // Display nationality | ||
| + | document.getElementById(" | ||
| + | "My father is " + myFather.nationality; | ||
| + | </ | ||
| + | \\ | ||
| + | 프로퍼티는 myMother가 아닌(다른 person 오브젝트가 아닌) myFather에 추가됩니다.\\ | ||
| + | \\ | ||
| + | =====Adding a Method to an Object===== | ||
| + | 기존의 오브젝트에 새로운 메서드를 추가하는 것은 쉽습니다: | ||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | // Constructor function for Person Objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | } | ||
| + | // Create 2 Person objects | ||
| + | let myFather = new Person(" | ||
| + | let myMother = new Person(" | ||
| + | |||
| + | // Add a name method to first object | ||
| + | myFather.name = function () { | ||
| + | return this.firstName + " " + this.lastName; | ||
| + | }; | ||
| + | |||
| + | // Display full Name | ||
| + | document.getElementById(" | ||
| + | "My father is " + myFather.name(); | ||
| + | </ | ||
| + | \\ | ||
| + | |||
| + | =====Adding a Property to a Constructor===== | ||
| + | 기존의 오브젝트에 새로운 프로퍼티를 추가한 방식으로 오브젝트 생성자에 새로운 프로퍼티를 추가할 수 없습니다.\\ | ||
| + | |||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | // Constructor function for Person objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | } | ||
| + | |||
| + | // You can NOT add a new property to a constructor function | ||
| + | Person.nationality = " | ||
| + | |||
| + | // Create 2 Person objects | ||
| + | let myFather = new Person(" | ||
| + | let myMother = new Person(" | ||
| + | |||
| + | // Display nationality | ||
| + | document.getElementById(" | ||
| + | "The nationality of my Father is " + myFather.nationality; | ||
| + | // The nationality of my Father is undefined | ||
| + | </ | ||
| + | \\ | ||
| + | 생성자에 새로운 프로퍼티를 추가하기 위해, 생성자 함수에 새로운 프로퍼티를 추가해야 합니다.\\ | ||
| + | \\ | ||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | // Constructor function for Person objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | this.nationality = " | ||
| + | } | ||
| + | |||
| + | // Create 2 Person objects | ||
| + | let myFather = new Person(" | ||
| + | let myMother = new Person(" | ||
| + | |||
| + | // Display nationality | ||
| + | document.getElementById(" | ||
| + | "My father is " + myFather.nationality + ". My mother is " + myMother.nationality; | ||
| + | // My father is English. My mother is English | ||
| + | </ | ||
| + | \\ | ||
| + | 위와 같은 방식으로 오브젝트 프로퍼티는 기본 값(default values)를 가질 수 있습니다.\\ | ||
| + | |||
| + | =====Adding a Method to a Constructor===== | ||
| + | 생성자 함수는 또한 메서드를 정의할 수 있습니다.\\ | ||
| + | |||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | // Constructor function for Person Objects | ||
| + | function Person(first, | ||
| + | this.firstName = first; | ||
| + | this.lastName = last; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eye; | ||
| + | this.name = function () { | ||
| + | return this.firstName + " " + this.lastName | ||
| + | }; | ||
| + | }; | ||
| + | |||
| + | // Create a Person object | ||
| + | let myFather = new Person(" | ||
| + | |||
| + | // Display full name | ||
| + | document.getElementById(" | ||
| + | "My father is " + myFather.name(); | ||
| + | </ | ||
| + | \\ | ||
| + | 기존의 오브젝트에 새로운 메서드를 추가한 방식과 동일하게 오브젝트 생성자에 새로운 메서드를 추가할 수 없습니다.\\ | ||
| + | \\ | ||
| + | 오브젝트 생성자에 메서드를 추가하는 것은 생성자 함수 내부에서 이루어저야 합니다.\\ | ||
| + | \\ | ||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | function Person(firstName, | ||
| + | this.firstName = firstName; | ||
| + | this.lastName = lastName; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eyeColor; | ||
| + | this.changeName = function (name) { | ||
| + | this.lastName = name; | ||
| + | }; | ||
| + | } | ||
| + | </ | ||
| + | \\ | ||
| + | changeName() 함수는 person의 lastName 프로퍼티에 이름 값을 할당합니다.\\ | ||
| + | \\ | ||
| + | ====Now You Can Try:==== | ||
| + | <code javascript> | ||
| + | // Constructor function for Person objects | ||
| + | function Person(firstName, | ||
| + | this.firstName = firstName; | ||
| + | this.lastName = lastName; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eyeColor; | ||
| + | this.changeName = function (name) { | ||
| + | this.lastName = name; | ||
| + | }; | ||
| + | } | ||
| + | |||
| + | // Create a Person object | ||
| + | let myMother = new Person(" | ||
| + | |||
| + | // Change last name | ||
| + | myMother.changeName(" | ||
| + | |||
| + | // Display last name | ||
| + | document.getElementById(" | ||
| + | "My mother' | ||
| + | </ | ||
| + | \\ | ||
| + | %%JavaScript%%는 **this**를 myMother로 " | ||
| + | \\ | ||
| + | |||
| + | =====Built-in JavaScript Constructors===== | ||
| + | %%JavaScript%%에는 네이티브 오브젝트에 대한 내장 생성자(built-in constructor)가 있습니다.\\ | ||
| + | \\ | ||
| + | |||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | let x1 = new Object(); | ||
| + | let x2 = new String(); | ||
| + | let x3 = new Number(); | ||
| + | let x4 = new Boolean(); | ||
| + | let x5 = new Array(); | ||
| + | let x6 = new RegExp(); | ||
| + | let x7 = new Function(); | ||
| + | let x8 = new Date(); | ||
| + | |||
| + | // Display the type of all objects | ||
| + | document.getElementById(" | ||
| + | "x1: " + typeof x1 + "< | ||
| + | "x2: " + typeof x2 + "< | ||
| + | "x3: " + typeof x3 + "< | ||
| + | "x4: " + typeof x4 + "< | ||
| + | "x5: " + typeof x5 + "< | ||
| + | "x6: " + typeof x6 + "< | ||
| + | "x7: " + typeof x7 + "< | ||
| + | "x8: " + typeof x8 + "< | ||
| + | </ | ||
| + | \\ | ||
| + | '' | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | =====Did You Know?===== | ||
| + | 위에서 볼 수 있듯이, %%JavaScript%%에는 원시 데이터 타입(primitive data types)인\\ | ||
| + | '' | ||
| + | 그러나 복잡한 오브젝트를 만들 이유가 없습니다. 원시 값(primitive values)이 훨씬 빠릅니다.\\ | ||
| + | \\ | ||
| + | 또한:\\ | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | '' | ||
| + | \\ | ||
| + | |||
| + | ====Example==== | ||
| + | <code javascript> | ||
| + | let x1 = {}; // object | ||
| + | let x2 = ""; | ||
| + | let x3 = 0; // number; | ||
| + | let x4 = false; | ||
| + | let x5 = []; // object (not array) | ||
| + | let x6 = /()/; // object | ||
| + | let x7 = function () { }; // function | ||
| + | |||
| + | // Display the type of all | ||
| + | document.getElementById(" | ||
| + | "x1: " + typeof x1 + "< | ||
| + | "x2: " + typeof x2 + "< | ||
| + | "x3: " + typeof x3 + "< | ||
| + | "x4: " + typeof x4 + "< | ||
| + | "x5: " + typeof x5 + "< | ||
| + | "x6: " + typeof x6 + "< | ||
| + | "x7: " + typeof x7 + "< | ||
| + | </ | ||
| + | \\ | ||
| + | |||
| + | =====String Objects===== | ||
| + | 일반적으로 문자열은 기본 형식('' | ||
| + | \\ | ||
| + | 그러나 '' | ||
| + | \\ | ||
| + | [[https:// | ||
| + | |||
| + | |||
| + | =====Number Objects===== | ||
| + | 일반적으로 숫자는 기본 형식('' | ||
| + | \\ | ||
| + | 그러나 '' | ||
| + | \\ | ||
| + | [[https:// | ||
| + | |||
| + | |||
| + | =====Boolean Objects===== | ||
| + | 일반적으로 불리언은 기본 형식(var x = false)으로 생성됩니다\\ | ||
| + | \\ | ||
| + | 그러나 불리언은 '' | ||
| + | \\ | ||
| + | [[https:// | ||
| + | 불리언이 오브젝트로 생성되지 않아야 하는 이유를 학습합니다.\\ | ||