문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
wiki:javascript:javascript_note:js_object_constructors [2021/05/03 13:00] emblim98 [Example] |
wiki:javascript:javascript_note:js_object_constructors [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 159: | 줄 159: | ||
| \\ | \\ | ||
| ====Example==== | ====Example==== | ||
| - | < | + | < |
| + | // 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 | ||
| </ | </ | ||
| \\ | \\ | ||
| 줄 170: | 줄 186: | ||
| ====Example==== | ====Example==== | ||
| <code javascript> | <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(); | ||
| </ | </ | ||
| \\ | \\ | ||
| 줄 179: | 줄 211: | ||
| ====Example==== | ====Example==== | ||
| <code javascript> | <code javascript> | ||
| + | function Person(firstName, | ||
| + | this.firstName = firstName; | ||
| + | this.lastName = lastName; | ||
| + | this.age = age; | ||
| + | this.eyeColor = eyeColor; | ||
| + | this.changeName = function (name) { | ||
| + | this.lastName = name; | ||
| + | }; | ||
| + | } | ||
| </ | </ | ||
| \\ | \\ | ||
| 줄 186: | 줄 226: | ||
| ====Now You Can Try:==== | ====Now You Can Try:==== | ||
| <code javascript> | <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' | ||
| </ | </ | ||
| \\ | \\ | ||
| 줄 198: | 줄 257: | ||
| ====Example==== | ====Example==== | ||
| <code javascript> | <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?===== | =====Did You Know?===== | ||
| 줄 226: | 줄 307: | ||
| ====Example==== | ====Example==== | ||
| <code javascript> | <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:// | |
| - | + | 불리언이 오브젝트로 생성되지 않아야 하는 이유를 학습합니다.\\ | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||