Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[15] 230117 Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด: 1. ๊ฐ์ฒด, 2. ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ, 3. ํ”„๋กœํ† ํƒ€์ž…, 4. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด, 5. Array ๊ฐ์ฒด, 6. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 15์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/JavaScript, jQuery, Ajax, JSON

[15] 230117 Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด: 1. ๊ฐ์ฒด, 2. ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ, 3. ํ”„๋กœํ† ํƒ€์ž…, 4. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด, 5. Array ๊ฐ์ฒด, 6. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 15์ผ]

yjyuwisely 2023. 1. 17. 12:50

230117 Tue 15th class

Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
์ง„๋„: p. 338 ~ (๊ต์žฌ: HTML5 + CSS3 + JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ, ์ €์ž: ์ฒœ์ธ๊ตญ)

 

HTML5+CSS3+JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ | ์ฒœ์ธ๊ตญ - ๊ต๋ณด๋ฌธ๊ณ 

HTML5+CSS3+JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ | ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ดˆ๋ณด์ž๋„ ์‰ฝ๊ฒŒ ๋ฐฐ์šฐ๋Š” ์›น ๊ฐœ๋ฐœ ์ง€์นจ์„œ!์ด ์ฑ…์€ ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ์ดˆ๋ณด์ž๋“ค์„ ์œ„ํ•ด ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์„ค๋ช…ํ•œ๋‹ค. 1~3์žฅ์—

product.kyobobook.co.kr


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

- ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•(Camel case ์นด๋ฉœ ์ผ€์ด์Šค[*]) ๋˜๋Š” ๋‚™ํƒ€ ํ‘œ๊ธฐ๋ฒ•
: ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํŒŒ์ผ, ๋ณ€์ˆ˜ํ•จ์ˆ˜ ๋“ฑ ๋Œ€์ƒ์˜ ์ด๋ฆ„์„ ๋„์–ด์“ฐ๊ธฐ ์—†์ด ์ง“๊ธฐ ์œ„ํ•˜์—ฌ ๋”ฐ๋ฅด๋Š” ๊ด€๋ก€์ธ ๋„ค์ด๋ฐ์ปจ๋ฒค์…˜(Naming convention)์˜ ํ•˜๋‚˜๋‹ค.

๋‹จ์–ด ์ „์ฒด์ ์œผ๋กœ ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๋งจ ์ฒซ ๊ธ€์ž๋ฅผ ์ œ์™ธํ•œ ๊ฐ ํ•ฉ์„ฑ์–ด์˜ ์ฒซ ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•œ๋‹ค. ํ•ฉ์„ฑํ•œ ๋‹จ์–ด์˜ ๋ชจ์–‘์ด ์Œ๋ด‰๋‚™ํƒ€์˜ ๋“ฑ๊ณผ ๋น„์Šทํ•˜๋‹ค๋Š” ๋œป์—์„œ ์ด๋ฆ„๋ถ™์—ˆ๋‹ค.
Common examples include "iPhone" and "eBay".

- The innerHTML property sets or returns the HTML content (inner HTML) of an element. 


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ 
- [] ๋ฐฐ์—ด
ex) var a = ["a", 1, "b"]
a.length = 3 (๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 3์ด๋‹ค.)

var s = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ" (๋ฌธ์ž์—ด)
s.length = 6 (๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 6์ด๋‹ค.)

- {} ๊ฐ์ฒด

- () ํ•จ์ˆ˜


- ๋ฆฌํ„ฐ๋Ÿด์€ ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ(๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜ ์•ˆ์˜ ๊ฐ’)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

- ๊ฐ์ฒด (Object): ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ํ•˜๋‚˜์˜ ์—”ํ„ฐํ‹ฐ(๋ฌถ์Œ)

- ์ƒ์ˆ˜ ๊ฐ์ฒด๋ž€ ๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝ ํ• ์ˆ˜ ์—†๋Š” ๊ฐ์ฒด์ด๋‹ค.
In computer science, a literal is a notation for representing a fixed value in source code.

- ํ•จ์ˆ˜์™€ ๋ฉ”์„œ๋“œ์˜ ์ฐจ์ด์ : ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ์œผ๋ฉด ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
ex) alert() ํ•จ์ˆ˜๋Š” window ๊ฐ์ฒด์˜ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค.

- ๊ฐ์ฒด ์•ˆ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
ex) document.write()
๊ฐ์ฒด.ํ•จ์ˆ˜()


2023.01.13 - [1. Revision 2023/Javascript] - [13] 230113 Ch. 8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ(5): ๋ฐฐ์—ด

๋ณ€์ˆ˜ (variable)
๋ฐฐ์—ด (Array)
๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ
ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ณต๊ฐ„. ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์—๋Š” ์˜ค๋กœ์ง€ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. 
๋Œ€์šฉ๋Ÿ‰์˜ ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ์ด๋‹ค.
๋ฐฐ์—ด์€ ์ „์ฒด ์ง‘๋‹จ์— ํ•˜๋‚˜์˜ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•œ ๋‹ค์Œ, ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ์— ์ˆซ์ž๋กœ ๋œ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์—ฌ์„œ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋งŽ์€ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์„ ํ• ๋‹น๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 
1. ์„ ์–ธ
var a = 10;// a๋ผ๋Š” ๋ณ€์ˆ˜์— 10์„ ์ €์žฅ
var b = 20;// a๋ผ๋Š” ๋ณ€์ˆ˜์— 20์„ ์ €์žฅ
var c = 30;// a๋ผ๋Š” ๋ณ€์ˆ˜์— 30์„ ์ €์žฅ

1. ์„ ์–ธ 
var a = [10,20,30]
2. ํ˜ธ์ถœ

b
c
2. ํ˜ธ์ถœ 
a[0]
a[1]
a[2]


Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด

์ฑ…์˜ ๋ชฉ์ฐจ
-๊ฐ์ฒด
-๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ
-ํ”„๋กœํ† ํƒ€์ž…
-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด
-Array ๊ฐ์ฒด
-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ

1. ๊ฐ์ฒด (Object) p. 330

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ์ž๋ฃŒํ˜•์ด๋‹ค.
๋ถˆ๋ณ€๊ฒฝ ๊ฐ์ฒด: ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๊ฐœ์ฒด ex) ๋ฌธ์ž์—ด(String), ์ˆ˜์น˜ํ˜•(Number), ๋ถ€์šธํ˜•(Boolean)

๊ฐ์ฒด๋Š” ์‹ค์ œ ์„ธ์ƒ์— ์กด์žฌํ•˜๋Š” ์‚ฌ๋ฌผ์„ ๋ชจ๋ธ๋งํ•œ ๊ฒƒ์ด๋‹ค.
๊ฐ์ฒด = ๋ฐ์ดํ„ฐ(data) + ๋™์ž‘(action)

์‚ฌ๋ฌผ์˜ ์†์„ฑ๊ณผ ๋™์ž‘์„ ๋ฌถ์–ด์„œ ํ‘œํ˜„ํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค.

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๋ฐ์ดํ„ฐ(๋ณ€์ˆ˜)์™€ ๋™์ž‘(ํ•จ์ˆ˜)์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค.



๊ฐ์ฒด (Object): ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ํ•˜๋‚˜์˜ ์—”ํ„ฐํ‹ฐ(๋ฌถ์Œ)

=

๊ฐ์ฒด ์•ˆ์˜ ๋ณ€์ˆ˜(๋ฐ์ดํ„ฐ): ์†์„ฑ(property)

๊ฐ์ฒด ์•ˆ์˜ ๋ณ€์ˆ˜(๋ฐ์ดํ„ฐ(data): ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŠน์„ฑ๊ฐ’): ์†์„ฑ(property)
ex) var model; var speed; var color;
ex) ๋ชจ๋ธ, ์†๋„, ์ƒ‰์ƒ, ์ œ์กฐ์‚ฌ, ๋งˆ๋ ฅ, ์—ฐ์‹, ๋ฐฐ๊ธฐ๋Ÿ‰, ์ฃผํ–‰ ๊ฑฐ๋ฆฌ, ์—ฐ๋น„


+

๊ฐ์ฒด ์•ˆ์˜ ํ•จ์ˆ˜(๋™์ž‘): ๋ฉ”์„œ๋“œ(method)

๊ฐ์ฒด ์•ˆ์˜ ํ•จ์ˆ˜(Function)(๋™์ž‘(action): ๊ฐ์ฒด๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ–‰๋™): ๋ฉ”์„œ๋“œ(method)
ex) function brake(speed){...}, function accel(speed){...}
ex) ๊ฐ€์†ํ•˜๊ธฐ, ๊ฐ์†ํ•˜๊ธฐ, ์ถœ๋ฐœํ•˜๊ธฐ, ๋ฐฉํ–ฅ ์ „ํ™˜ํ•˜๊ธฐ, ๊ธฐ์•„ ๋ณ€์†ํ•˜๊ธฐ



 

function brake(speed){ //๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ
...
}
var model; //๊ฐ์ฒด์˜ ์†์„ฑ
var speed; //๊ฐ์ฒด์˜ ์†์„ฑ
var color; //๊ฐ์ฒด์˜ ์†์„ฑ

function accel(speed){ //๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ
...
}


ํ•จ์ˆ˜์™€ ๋ฉ”์„œ๋“œ์˜ ์ฐจ์ด์ : ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ์œผ๋ฉด ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
ex) alert() ํ•จ์ˆ˜๋Š” window ๊ฐ์ฒด์˜ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค.


2 ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ p. 332

2.1 ๊ฐ์ฒด์˜ ์ข…๋ฅ˜

1) ๋‚ด์žฅ ๊ฐ์ฒด (built-in object): ์ƒ์„ฑ์ž๊ฐ€ ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค.
(์ƒ์„ฑ์ž: ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ
์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”(๋ณ€์ˆ˜์— ์ œ์ผ ์ฒ˜์Œ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’ ex) var a = 10;) ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.)

ex) Date, String, Array

ex) document.write("์•ˆ๋…•");
๊ฐ์ฒด ํ•จ์ˆ˜

document: HTML ๋ฌธ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‚ด์žฅ ๊ฐ์ฒด์ด๋‹ค. Document ๊ฐ์ฒด๋Š” HTML ์š”์†Œ์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ๋„์™€์ฃผ๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
write: ํ•จ์ˆ˜

2) ์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด (custom obejct) : ์‚ฌ์šฉ์ž๊ฐ€ ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•œ๋‹ค.


2.2 ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•

1) ๊ฐ์ฒด ์ƒ์ˆ˜(object literal)๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด ์ƒ์„ฑ

๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด ์ƒ์ˆ˜๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ์ƒ์„ฑํ•œ๋‹ค.
(์ƒ์ˆ˜ ๊ฐ์ฒด๋ž€ ๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝ ํ• ์ˆ˜ ์—†๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.
In computer science, a literal is a notation for representing a fixed value in source code.)


์‹ฑ๊ธ€ํ†ค(singleton): ๊ฐ์ฒด๊ฐ€ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋œ๋‹ค.

์˜ˆ์‹œ1)

//์ž๋™์ฐจ๋ผ๋Š” ๊ฐ์ฒด
//์ž๋™์ฐจ์™€ ๊ด€๋ จ๋œ ๋ณ€์ˆ˜ (model, speed, color)
var model = "520d";
var speed = 60;
var coloar = "red";

//์ž๋™์ฐจ์™€ ๊ด€๋ จ๋œ ํ•จ์ˆ˜
function brake(){
speed -= 10; //speed = speed - 10;
}

function excel(){
speed += 10; //speed = speed + 10;
}

์˜ˆ์‹œ2)

var car = {
//๊ฐ์ฒด์—์„œ์˜ ๋ณ€์ˆ˜ ์„ ์–ธ
model: "520d",
speed: 60,
color: "red",
//๊ฐ์ฒด์—์„œ์˜ ํ•จ์ˆ˜ ์„ ์–ธ
brake: function(){this.speed -= 10; return this.speed},//speed = speed-10;
excel: function(){this.speed += 10;}//speed = speed+10;
}

car.speed=70;
document.writeln(car.speed+"<br>");
document.writeln(car.brake());

๊ฒฐ๊ณผ)

70
60

 

2) ์ƒ์„ฑ์ž ํ•จ์ˆ˜(Constructor Function)๋ฅผ ์ด์šฉํ•œ ๊ฐ์ฒด ์ƒ์„ฑ

์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด: ๊ฐœ๋ฐœ์ž๋Š” ์ž์‹ ๋งŒ์˜ ๊ฐ์ฒด๋„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ์ƒ์„ฑ์ž๋ผ๋Š” ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”(๋ณ€์ˆ˜์— ์ œ์ผ ์ฒ˜์Œ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’ ex) var a = 10;) ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ฐ์‚ฐ์ž(์–ด๋–ค ํ•จ์ˆ˜์— ์ž‘์šฉํ•ด ๊ทธ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ๋ณ€ํ˜•์‹œํ‚ค๋Š” ํ•จ์ˆ˜)๋Š” new์ด๋‹ค.

 ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•˜๊ณ  new๋ฅผ ํ†ตํ•˜์—ฌ ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
An instance is a specific realization of any object in object-oriented programming.

new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑ์ž(constructor)๋ผ๊ณ  ํ•˜๋ฉฐ,
์ด ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์›์‹œ ํƒ€์ž…์„ ์œ„ํ•œ ์ƒ์„ฑ์ž๋ฅผ ๋ฏธ๋ฆฌ ์ •์˜ํ•˜์—ฌ ์ œ๊ณตํ•œ๋‹ค.
(์›์‹œ ํƒ€์ž…์€ ์ •์ˆ˜, ์‹ค์ˆ˜, ๋ฌธ์ž, ๋…ผ๋ฆฌ ๋ฆฌํ„ฐ๋Ÿด ๋“ฑ ์‹ค์ œ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ํƒ€์ž…์ด๋‹ค.)

์ฐธ๊ณ : https://developer-talk.tistory.com/281



์˜ˆ์‹œ1)

var day = new Date(); // new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Date ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ.
document.write("์˜ฌํ•ด๋Š” " + day.getFullYear() + "๋…„์ž…๋‹ˆ๋‹ค.");

this๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํ˜„์žฌ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ƒ์„ฑ์ž๋Š” ์ „๋‹ฌ๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ’์œผ๋กœ ํ˜„์žฌ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์„ค์ •ํ•œ๋‹ค.
this๊ฐ€ ์—†๋‹ค๋ฉด ์†์„ฑ์„ ์ง€์—ญ ๋ณ€์ˆ˜๋กœ ํ˜ผ๋™ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์˜ ์†์„ฑ์—๋Š” ๋ฐ˜๋“œ์‹œ this๋ฅผ ๋ถ™์ธ๋‹ค.
์•ž์— this๊ฐ€ ๋ถ™์ธ ๋ณ€์ˆ˜ = ์†์„ฑ
์•ž์— this๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜ = ๋ฉ”์„œ๋“œ (ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ์œผ๋ฉด ๋ฉ”์„œ๋“œ)

๋‚ด์žฅ ๊ฐ์ฒด๋Š” ๋ฏธ๋ฆฌ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ž‘์„ฑ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด new ํ•˜์—ฌ์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์‹œ2)

function Car(model, speed, color){ //Car ์ƒ์„ฑ์ž ์„ ์–ธ
// ์†์„ฑ
this.model=model;
this.speed=speed;
this.color=color;

// ํ•จ์ˆ˜
brake = function(){
this.speed -= 10; // this.speed = this.speed - 10
}

excel = function(){
this.speed += 10; // this.speed = this.speed + 10
}
}
//Car ์ƒ์„ฑ์ž ํ˜ธ์ถœ
mycar = new Car("520d", 60, "red"); //๊ฐ model, speed, color
//์ƒ์„ฑ์ž ์ด์šฉํ•ด์„œ ๊ฐ’ ๋ฐ”๊พผ๋‹ค.
mycar = new Car("520d", 60, "yellow");
document.write("๋ชจ๋ธ: "+mycar.model+" ์†๋„: "+mycar.speed+"<br>")


์˜ˆ์‹œ3) ๊ฐ์ฒด ์ด์šฉ ๊ณ„์‚ฐ๊ธฐ

์ฐธ๊ณ : https://ko.javascript.info/array-methods


2.3 ๊ฐ์ฒด ๋ฉค๋ฒ„ ์‚ฌ์šฉํ•˜๊ธฐ

๊ฐ์ฒด ๋ฉค๋ฒ„๋Š” ๊ฐ์ฒด ์•ˆ์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ์ด๋‹ค.
.(dot)์„ ์‚ฌ์šฉํ•œ๋‹ค.
๋จผ์ € ์–ด๋–ค ๊ฐ์ฒด์— ์†ํ•ด ์žˆ๋Š”์ง€ ๋งํ•˜๊ณ  ์ด๋ฆ„์„ ๋งํ•œ๋‹ค.
ex) 

myCar.color = "red";


๊ฐ์ฒด ์ด๋ฆ„, ์†์„ฑ ์ด๋ฆ„
myCar.brake();
๊ฐ์ฒด ์ด๋ฆ„, ๋ฉ”์„œ๋“œ ์ด๋ฆ„


3. ํ”„๋กœํ† ํƒ€์ž… p. 338

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ํ”„๋กœํ† ํƒ€์ž…(prototype)์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
๋ชจ๋“  ๊ฐ์ฒด๋Š” ๊ทธ๋“ค์˜ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ๋ถ€ํ„ฐ ํ”„๋กœํผํ‹ฐ(์†์„ฑ)์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
์ด์ฒ˜๋Ÿผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ตœ์†Œํ•œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ƒ์†์„ ๋ฐ›์œผ๋ฉฐ, ์ด๋•Œ ์ƒ์†๋˜๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ํ”„๋กœํ† ํƒ€์ž…(prototype)์ด๋ผ๊ณ  ํ•œ๋‹ค.
์ฐธ๊ณ : http://www.tcpschool.com/javascript/js_object_prototype

* ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” prototype์ด๋ผ๋Š” ์ˆจ๊ฒจ์ง„ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ๊ณต์œ ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

ํ•˜๋‚˜์˜ ๊ฐœ๋ณ„์ ์ธ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” 

์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ํ•„์š”ํ•˜๋‹ค.
์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋˜์–ด์„œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์˜€๋‹ค.

์˜ˆ์‹œ)

function Point(xpos, ypos){ // ์ƒ์„ฑ์ž() ๋Œ€๋ฌธ์ž Point์ด๋ฏ€๋กœ ์ƒ์„ฑ์ž๋กœ ์ธ์‹ํ•œ๋‹ค.
// ์•„๋ž˜์˜ ๋‘ ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐํ™”๋ฅผ ์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

this.x = xpos; // ๋ณ€์ˆ˜
this.y = ypos; // ๋ณ€์ˆ˜
this.getDistance = function(){ // ํ•จ์ˆ˜
return Math.sqrt(this.x * this.x + this.y * this.y);
};
}

var p1 = new Point(10, 20);
var d1 = p1.getDistance();

var p2 = new Point(10, 30);
var d2 = p2.getDistance();

document.writeln(d1+"<br>");
document.writeln(d2+"<br>");

๊ฒฐ๊ณผ)
22.360679774997898
31.622776601683793



3.1 ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ (prototype chain) p.342

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋Œ€๋กœ ์ฐพ๋Š”๋‹ค. 

1. ๊ฐ์ฒด ์•ˆ์— ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.

2. ๊ฐ์ฒด ์•ˆ์— ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ๊ฐ์ฒด์˜ prototype์ด ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.


3. ์›ํ•˜๋Š” ์†์„ฑ/๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ํ”„๋กœํ†  ํƒ€์ž… ์ฒด์ธ(chain)์„ ๋”ฐ๋ผ์„œ ์˜ฌ๋ผ๊ฐ„๋‹ค. 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ํ”„๋กœํ† ํƒ€์ž…์„ ์ด์šฉํ•ด ์ƒ์†์„ ์–ด๋Š ์ •๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.  


4. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด p.343

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด๋งŒ ์‚ฌ์šฉํ•ด๋„ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค. 

4.1 Date ๊ฐ์ฒด

Date ๊ฐ์ฒด๋Š” ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ž‘์—…์„ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ, ์ƒ๋‹นํžˆ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด์ด๋‹ค.
์ƒˆ๋กœ์šด Date ๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑํ•œ๋‹ค.

var today = new Date(); //ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

4.1.1 Date ๊ฐ์ฒด ์ƒ์„ฑ์ž 

Date ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋ฌธ์ž์—ด์„ ์ธ์ˆ˜๋กœ ์ค„ ์ˆ˜ ์žˆ๊ณ , ์ •์ˆ˜๋ฅผ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

์˜ˆ์‹œ1)

new Date(); //ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„
new Date(milliseconds); //1970/01/01 ์ดํ›„์˜ ๋ฐ€๋ฆฌ์ดˆ
new Date(dateString); //๋‹ค์–‘ํ•œ ๋ฌธ์ž์—ด
new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])

์˜ˆ์‹œ2) ์›”์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. 

var d1 = new Date(2023, 7, 24); //2023๋…„ 8์›” 24์ผ
var d2 = new Date(2023, 7, 24, 18, 30, 0);
var d3 = new Date(2023, 7, 24, 18, 30, 0, 0);

์˜ˆ์‹œ3)

var d1 = new Date(2023, 7, 21, 0, 0, 0);
var d2 = new Date("January 20, 2023 11:13:00");
alert(d1);
alert(d2);

๊ฒฐ๊ณผ)


4.1.2 Date์˜ ๋ฉ”์„œ๋“œ

์˜ˆ์‹œ)

var date = new Date(2022,10,25,17,30,35);
//alert(today);
document.write(date.getDate()+"<br>"); //getDate()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ ์งœ ํ˜•์‹ ์ค‘์—์„œ ์ผ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
//25
document.write(date.getDay()); //getDate()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ ์งœ ํ˜•์‹ ์ค‘์—์„œ ์š”์ผ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
//0~6 0:์ผ์š”์ผ


๊ฒฐ๊ณผ)


4.1.3 ๋‘ ๊ฐœ์˜ ๋‚ ์งœ ๋น„๊ตํ•˜๊ธฐ p. 347

Date ๊ฐ์ฒด๋Š” ๋‘ ๊ฐœ์˜ ๋‚ ์งœ๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐ๋„ ์‚ฌ์šฉ๋œ๋‹ค. ex) ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋‚ ์งœ๋ฅผ ํŠน์ •ํ•œ ๋‚ ์งœ์™€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.

๋จผ์ € ๋ชจ๋“  ๋‚ ์งœ๋ฅผ 1970๋…„ 1์›” 1์ผ ์ดํ›„๋กœ ๋ฐ€๋ฆฌ์ดˆ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. getTime()์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
๋‚ ์งœ ๊ฐ„๊ฒฉ์„ ๊ตฌํ•  ๋•Œ๋Š” ๋ฐ€๋ฆฌ์ดˆ์˜ ์ฐจ์ด๊ฐ’์„ (1000*60*60*24)๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

(๋ฐ€๋ฆฌ์ดˆ(millisecond, ms)๋Š” ์ฒœ ๋ถ„์˜ 1์ดˆ
1 ์ดˆ [s] = 1000 ๋ฐ€๋ฆฌ์ดˆ [ms])

1์ผ์€ 1000 * 60 * 60 * 24 = 86,400,000 ๋ฐ€๋ฆฌ์ดˆ
1์‹œ๊ฐ„์€ 1000 * 60 * 60 = 3,600,000 ๋ฐ€๋ฆฌ์ดˆ
1๋ถ„์€ 1000 * 60 = 60000๋ฐ€๋ฆฌ์ดˆ
1์ดˆ๋Š” 1000 ๋ฐ€๋ฆฌ์ดˆ

์˜ˆ์‹œ)

<script>
function checkDate(){
//id๊ฐ€ "pdate"์ธ ์š”์†Œ์˜ value์†์„ฑ์„ ์ฝ์–ด์ค€๋‹ค. 
var s = document.getElementById("pdate").value; //2023-01-01
//alert(s);
//ํ˜•์‹์„ ๊ฐ™๊ฒŒ ๋ฐ”๊พผ๋‹ค. 
var pdate = new Date(s); // Sun Jan 01 2023 09:00:00 GTM+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
//alert(pdate);
var today = new Date(); // Tue Jan 17 2023 10:09:28 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
//alert(today.getTime);
//Math.floor: ๋‚ด๋ฆผ
var diff = Math.floor(diff / (1000*60*60*24));

if (days > 30){
alert("๊ตํ™˜ ๊ธฐํ•œ์ด ์ง€๋‚ฌ์Šต๋‹ˆ๋‹ค.");
}
}
</script>
</head>

<body>
๊ตฌ์ž…๋‚ ์งœ :
<input type="date" id="pdate">
<button onclick="checkDate()">๊ฒ€์‚ฌ</button>
</body>


๊ฒฐ๊ณผ)


4.1.4 ์นด์šดํŠธ๋‹ค์šด ํƒ€์ด๋จธ ๋งŒ๋“ค๊ธฐ p. 348

- The innerHTML property sets or returns the HTML content (inner HTML) of an element.
<div id='remaining'> (innerHTML) </div> ์—์„œ ์‚ฌ์ด์— ๋น„์–ด ์žˆ๋Š” ๊ณณ์ด innerHTML์ด๋‹ค.
์—ด๊ณ  ๋‹ซ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. <input type="text"></input>์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ด ์•ˆ ๋œ๋‹ค. 

The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.

์˜ˆ์‹œ) ํŠน์ • ๋‚ ์งœ๊นŒ์ง€ ๋‚จ์€ ๋‚ ์งœ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์ฝ”๋“œ
๋‚ด๋…„ 1์›” 1์ผ๊นŒ์ง€ ๋‚จ์€ ๋‚ ์งœ๋ฅผ ์›น ํŽ˜์ด์ง€์— ํ‘œ์‹œํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

<div id='remaining'> </div>
<script>
function datesUntilNewYear(){//starts

var now = new Date(); //์˜ค๋Š˜ ๋‚ ์งœ
var newYear = new Date('January 1,' + (now.getFullYear() + 1));
var diff = newYear - now;

//Math.floor(): ์†Œ์ˆ˜์  ๋ฒ„๋ฆผ 
var milliseconds = Math.floor(diff % 1000);
diff = diff / 1000;

var seconds = Math.floor(diff % 60);
diff = diff / 60; 

var minutes = Math.floor(diff % 60);
diff = diff / 60; 

var hours = Math.floor(diff % 24);
diff = diff / 24;

var days = Math.floor(diff);

var outStr = '๋‚ด๋…„๋„ ์‹ ์ •๊นŒ์ง€ ' + days + '์ผ, ' + hours + '์‹œ๊ฐ„, ' + minutes;
outStr += '๋ถ„, ' + seconds + '์ดˆ ' + '๋‚จ์•˜์Šต๋‹ˆ๋‹ค.';

document.getElementById('remaining').innerHTML = outStr;
//1์ดˆ๊ฐ€ ์ง€๋‚˜๋ฉด ๋‹ค์‹œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 1์ดˆ๋งˆ๋‹ค datesUntilNewYear ํ•จ์ˆ˜ ์‹คํ–‰ํ•œ๋‹ค. 
setTimeout("datesUntilNewYear()", 1000);
}//ends

//ํƒ€์ด๋จธ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
datesUntilNewYear();

๊ฒฐ๊ณผ)


4.1.5 ์‹œ๊ณ„ ๋งŒ๋“ค๊ธฐ p. 350

์ƒˆ๋กœ์šด Date ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹œ๊ฐ„, ๋ถ„, ์ดˆ๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์‹œ)

<!-- ์‚ฌ์ด์— inner.HTML์ด ์žˆ๋‹ค.  -->
<div id="clock"></div>

<script>
function setClock(){
var now = new Date();
//์‹œ๊ฐ„, ๋ถ„, ์ดˆ๋งŒ ๋œจ๊ฒŒ ํ•œ๋‹ค.
var s = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();

//s ๋Œ€์‹  now๋กœ ํ•˜๋ฉด ์ „์ฒด๊ฐ€ ๋‚˜์˜จ๋‹ค.
document.getElementById("clock").innerHTML = now;

//1000: ๋‘๋ฒˆ์งธ ์ธ์ˆ˜์˜ ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ)๋งŒํผ ์ง€๋‚œ ํ›„์— ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
//์ฆ‰ ์—ฌ๊ธฐ์„œ๋Š” 1์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„์— setClock()์„ ํ˜ธ์ถœํ•œ๋‹ค. 
setTimeout("setClock()",1000);
}

setClock();
</script>

๊ฒฐ๊ณผ)


4.2 Number ๊ฐ์ฒด p. 350

Number ๊ฐ์ฒด๋Š” ์ˆ˜์น˜ํ˜• ๊ฐ’์„ ๊ฐ์‹ธ์„œ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๋žฉํผ(wrapper) ๊ฐ์ฒด์ด๋‹ค.
๋žฉํผ ๊ฐ์ฒด๋ž€ ์ˆ˜์น˜๊ฐ’์„ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๊ณ  ๋ฐ˜๋“œ์‹œ ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.

Number ๊ฐ์ฒด๋Š” new Number()๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
var num = new Number(7);

๋‹จ์ˆœํ•œ ์ˆ˜์น˜๊ฐ’์—๋„ ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž๋™์ ์œผ๋กœ ์ˆซ์ž์— ๋žฉํผ ๊ฐ์ฒด๋ฅผ ๊ฐ์‹ธ์„œ ๊ฐ์ฒด๋กœ ๋งŒ๋“ ๋‹ค.
1.234.toString(); // 1.234๋ฅผ ๊ฐ์‹ธ๋Š” ๋žฉํผ ๊ฐ์ฒด๊ฐ€ ํ˜•์„ฑ๋œ๋‹ค.

Number ๊ฐ์ฒด์˜ ์†์„ฑ ์ค‘ ์ผ๋ถ€:
ex) MAX VALUE - ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ’
MIN VALUE - ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’
Nan - "Not-a-Number"์˜ ์•ฝ์ž 

์˜ˆ์‹œ)

var count1, count2;
count1 = new Number(1.237);
count2 = 1.238;

if(count1.toFixed(2) == count2.toFixed(2)){
alert("์†Œ์ˆ˜์  2์งธ ์ž๋ฆฌ๊นŒ์ง€ ๊ฐ™์Šต๋‹ˆ๋‹ค.");
}

๊ฒฐ๊ณผ)


4.3 String ๊ฐ์ฒด p.352

์˜ˆ์‹œ)

<script>
function checkID(){
var s = document.getElementById("id").value;
//s.length: ๊ธ€์ž ์ˆ˜ (= ๋ฌธ์ž์—ด ๊ธธ์ด)
if (s.length < 6){
alert("์•„์ด๋””๋Š” 6๊ธ€์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
}
</script>
</head>

<body>
์•„์ด๋””: <input type="text" id="id" size=10>
<button onclick="checkID()">๊ฒ€์‚ฌ</button>
</body>

๊ฒฐ๊ณผ)


4.3.1 ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

toUpperCase()์™€toLowerCase()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด์˜ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž ํ˜น์€ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์‹œ)

var s = "aBcDeF";
var result1 = s.toLowerCase();
var result2 = s.toUpperCase();
document.writeln(result1);
document.writeln(result2+"<br>");

๊ฒฐ๊ณผ)


4.3.2 ๋ฌธ์ž์—ด ๋ถ™์ด๊ธฐ

concat() ๋ฉ”์„œ๋“œ๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๊ณผ ํ•ฉ์นœ๋‹ค.
ex) ์ฃผ์†Œ ์ž…๋ ฅ์นธ์ด 2๊ฐœ์ธ๋ฐ db ๋„ฃ์„ ๋•Œ ํ•ฉ์นœ๋‹ค. (์—ฐ๊ฒฐ์‹œํ‚จ๋‹ค.)

ex) ์ƒ๋…„์›”์ผ์„ ๋”ฐ๋กœ ์ž…๋ ฅํ•˜๋”๋ผ๋„ ์ž๋™์ ์œผ๋กœ db์— ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ๋ณด๋‚ธ๋‹ค. 

๊ฒฐ๊ณผ๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์ด ๋œ๋‹ค. 

+ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 

์˜ˆ์‹œ) 

var s1 = "๋ฌธ์ž์—ด1";
var s2 = "๋ฌธ์ž์—ด2";

//concat: ์•ž์˜ ๋ฌธ์ž์—ด๊ณผ ๋’ค์˜ ๋ฌธ์ž์—ด์„ ์ด์–ด์ค€๋‹ค. s1 + s2 ์™€ ๊ฐ™๋‹ค.
var s3 = s1.concat(s2);
document.write(s3);

๊ฒฐ๊ณผ)


4.3.3 ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰

indexOf() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

์˜ˆ์‹œ)

//๋„์–ด์“ฐ๊ธฐ ํ•œ ๊ฒƒ๋„ ํฌํ•จ๋œ๋‹ค. 
//       1234567891011121314151617181920
var s = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ์˜ค์‹  ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.";
var n = s.indexOf("ํ™˜์˜");

//15
document.write(n);

๊ฒฐ๊ณผ)
15


4.3.4 ๋ฌธ์ž์—ด ๋งค์นญ p. 355

match() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ผ์น˜ํ•˜๋Š” ์ฝ˜ํ…์ธ ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. 
์ •๊ทœ์‹(regular expression)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ?, *, ^ ๋“ฑ์˜ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์‹œ)

var str = "Like father, like son.";

//like๋ฅผ ์ฐพ๋Š”๋‹ค. 
//i๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ต์…˜์ด๋‹ค.
//g๋Š” ๊ฒ€์ƒ‰๋œ ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์คŒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ต์…˜์ด๋‹ค.
result = str.match(/like/ig);
document.write(result);

๊ฒฐ๊ณผ)


4.3.5 ๋ฌธ์ž์—ด ๋Œ€์ฒด

replace() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ฃผ์–ด์ง„ ๊ฐ’์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค. ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ex) ์ „ํ™”๋ฒˆํ˜ธ ์ž…๋ ฅ ๋•Œ - ๋ฅผ ๋นˆ์นธ์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค. 
i๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋Œ€์†Œ๋ฌธ์ž์— ๊ด€๊ณ„์—†์ด ๋Œ€์ฒดํ•œ๋‹ค. 
ex) result = s.replace(/Hong's/i, "Kim's");

์˜ˆ์‹œ)

var s = "Hong's number is 123-4567.";
//s ๋ฌธ์ž์—ด ์ค‘์—์„œ Hong's -> Kim's ๋ณ€ํ™˜
result = s.replace("Hong's","Kim's");
document.write(result);

๊ฒฐ๊ณผ)


4.3.6 split(delimiter[, limit])

split()๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋ถ„๋ฆฌ์ž๋กœ ํ•˜์—ฌ์„œ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•œ ํ›„์— ๊ฐ ํ•ญ๋ชฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
split()์„ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์‹œ) ๋ถ„๋ฆฌ์ž = ","
array.length = 5 (๋ฐฐ์—ด์˜ ๊ธธ์ด) 

s = "One, Two, Three, Four, Five";
var array = s.split(",");
for (i = 0; i < array.length; i++){
document.write(i + '-' + array[i] + '<br>');
}

๊ฒฐ๊ณผ)


4.3.7 HTML ๋žฉํผ ๋ฉ”์„œ๋“œ

๋ฌธ์ž์—ด์„ ์ ์ ˆํ•œ HTML ํƒœ๊ทธ๋กœ ๊ฐ์‹ผ ํ›„์— ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์˜ˆ์‹œ)

var s = "This is a test.";
document.write("Big: " + s.big() + "<br>");
document.write("Small: " + s.small() + "<br>");
document.write("Bold: " + s.bold() + "<br>");
document.write("Italic: " + s.italics() + "<br>");
document.write("Fixed: " + s.fixed() + "<br>");
document.write("Strike: " + s.strike() + "<br>");
document.write("Fontcolor: " + s.fontcolor() + "<br>");
document.write("Fontsize: " + s.fontsize() + "<br>");
document.write("Subscript: " + s.sub() + "<br>");
document.write("Superscript: " + s.sup() + "<br>");
document.write("Link " + s.link("http://www.google.com") + "<br>");

๊ฒฐ๊ณผ)


4.3.8 ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๊ณผ ๋ฌธ์ž์—ด ๊ฐ์ฒด p. 356

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฌธ์ž์—ด(String)์—๋Š” 2๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

1) ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด(String Literal): ํฐ๋”ฐ์˜ดํ‘œ๋กœ ์ƒ์„ฑ๋œ๋‹ค.
๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋„ ๋ฌธ์ž์—ด ๊ฐ์ฒด์˜ ๋ชจ๋“  ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž„์‹œ์ ์œผ๋กœ ๋ฌธ์ž์—ด ์ƒ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด ๊ฐ์ฒด๋กœ ํ˜•๋ณ€ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

var s = "์•ˆ๋…•";



2) ๋ฌธ์ž์—ด ๊ฐ์ฒด(String Object): ํ‚ค์›Œ๋“œ new๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑ๋œ๋‹ค. 
๋ฌธ์ž์—ด ๊ฐ์ฒด๊ฐ€ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋  ๋•Œ๋Š” ๊ฐ’(value)๋งŒ ์ „๋‹ฌ๋œ๋‹ค. ์ฆ‰ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋”๋ผ๋„ ์›๋ณธ์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. 

var o = new String("์•ˆ๋…•");

4.4 Math ๊ฐ์ฒด p. 358

Math ๊ฐ์ฒด๋Š” ์ˆ˜ํ•™์ ์ธ ์ž‘์—…์„ ์œ„ํ•œ ๊ฐ์ฒด์ด๋‹ค. Math๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐ”๋กœ ๊ฐ์ฒด์ด๋‹ค.
๋”ฐ๋ผ์„œ new๋ฅผ ํ†ตํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ํ•„์š” ์—†์ด ๋ฐ”๋กœ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.
์ˆ˜ํ•™ ๊ณ„์‚ฐ์— ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜์™€ ๊ฑฐ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ๋ด์•ผ ํ•œ๋‹ค. 

- ํ•จ์ˆ˜

์†์„ฑ ์„ค๋ช…
E ์˜ค์ผ๋Ÿฌ์˜ ์ƒ์ˆ˜ (์•ฝ 2.718)
LN2 ์ž์—ฐ ๋กœ๊ทธ (๋ฐ‘์ˆ˜: 2) (์•ฝ 0.693)
LN10 ์ž์—ฐ ๋กœ๊ทธ (๋ฐ‘์ˆ˜: 10) (์•ฝ 2.302)
PI ํŒŒ์ด ์ƒ์ˆ˜ (์•ฝ 3.14)
SQRT1_2 1/2 (์•ฝ 0.707)
SQRT2 2์˜ ์ œ๊ณฑ๊ทผ (์•ฝ 1.414)

- ๋ฉ”์„œ๋“œ

์†์„ฑ ์„ค๋ช…
abs(x) ์ ˆ๋Œ€๊ฐ’
aocs(x), asin(x), atan(x) ์•„ํฌ ์‚ผ๊ฐํ•จ์ˆ˜
ceil(x), floor(x) ์‹ค์ˆ˜๋ฅผ ์ •์ˆ˜๋กœ ์˜ฌ๋ฆผ, ๋‚ด๋ฆผ ํ•จ์ˆ˜
cos(x), sin(x), tan(x) ์‚ผ๊ฐํ•จ์ˆ˜
exp(x) ์ง€์ˆ˜ํ•จ์ˆ˜
log(x) ๋กœ๊ทธํ•จ์ˆ˜
max(x,y,z,...,n) ์ตœ๋Œ€๊ฐ’
min(x,y,z,...,n) ์ตœ์†Œ๊ฐ’
pow(x,y) ์ง€์ˆ˜ํ•จ์ˆ˜ x^y
random() 0๊ณผ 1์‚ฌ์ด์˜ ๋‚œ์ˆ˜๊ฐ’ ๋ฐ˜ํ™˜
round(x) ๋ฐ˜์˜ฌ๋ฆผ
sqrt(x) ์ œ๊ณฑ๊ทผ

 

์˜ˆ์‹œ) ๊ณตํ•™์šฉ ๊ณ„์‚ฐ๊ธฐ
ํ•จ์ˆ˜๋Š” ์„ ์–ธํ•˜๊ณ  ํ˜ธ์ถœํ•œ๋‹ค. 

<style>
body{text-align:center;}
</style>
<title>Math</title>
<script>
//ํ‚ค๋ณด๋“œ์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ์ˆœ๊ฐ„ ๊ฐ€์ ธ๊ฐ event๋Š” ๋ฐ”๋€œ
//document.getElementById("in").value
//๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ์ˆœ๊ฐ„ ์ž…๋ ฅํ•œ ๊ฐ’์„ ๊ฐ€์ ธ๊ฐ€์„œ ๊ณ„์‚ฐํ•จ

function calc(type){//starts
//์ž…๋ ฅ ์–‘์‹์˜ number1 ๊ฐ์ฒด์˜ value ๊ฐ’์„ Number์„ ์ด์šฉํ•ด ์ ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
//Number, ParseInt: ๋ฌธ์ž์—ด -> ์ˆซ์ž 
//var x = Number(document.getElementById("in").value);
//document: form ํƒœ๊ทธ ์ „์ฒด, name = calculator, name = number1 
var x = Number(document.calculator.number1.value);

//๋งŒ์•ฝ์— type์ด 1์ด๋ฉด, sin์„ ๊ตฌํ•˜๋ผ.
if (type == 1)
//Math ๊ฐ์ฒด์— ์žˆ๋Š” sinํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ 
y = Math.sin((x * Math.PI) / 180);

//๋งŒ์•ฝ์— type์ด 2์ด๋ฉด, log์„ ๊ตฌํ•˜๋ผ.
else if (type == 2)
y = Math.log(x);

//๋งŒ์•ฝ์— type์ด 3์ด๋ฉด, sqrt์„ ๊ตฌํ•˜๋ผ.
else if (type == 3)
y = Math.sqrt(x);

//๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด abs์„ ๊ตฌํ•˜๋ผ.
else if (type == 4) 
y = Math.abs(x);

//์ž…๋ ฅ ์–‘์‹์˜ total ๊ฐ์ฒด์˜ value ๊ฐ’์„ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋กœ ์„ค์ •ํ•œ๋‹ค.
document.calculator.total.value = y;
}//ends
</script>
</head>

<body>
<form name="calculator">
์ž…๋ ฅ: <input type="text" name="number1"><br>
๊ณ„์‚ฐ ๊ฒฐ๊ณผ: <input type="text" name="total"><br>
<!-- cal(1): ํ•จ์ˆ˜ ์ด๋ฆ„์œผ๋กœ ๋˜์–ด์žˆ์œผ๋ฉด ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด๋‹ค. */  --> 
<input type="button" value="SIN" onclick="calc(1);">
<input type="button" value="LOG" onclick="calc(2);">
<input type="button" value="SQRT" onclick="calc(3);">
<input type="button" value="ABS" onclick="calc(4);">
</form>
</body>

 

๊ฒฐ๊ณผ)


5. Array ๊ฐ์ฒด p. 360

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋ฐฐ์—ด๋„ Array๋ผ๋Š” ๊ฐ์ฒด๋กœ ํ‘œํ˜„๋œ๋‹ค.
Array ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด ์ƒ์„ฑ, ๋ฐฐ์—ด์— ๋Œ€ํ•˜์—ฌ ์ •๋ ฌ๊ณผ ๊ฐ™์€ ์—ฐ์‚ฐ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์€ Array ๊ฐ์ฒด๋ฅผ new ์—ฐ์‚ฐ์ž๋กœ ์ƒ์„ฑํ•˜๊ณ  ๋ฐฐ์—ด ์š”์†Œ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์ด๋‹ค.

//Array ๊ฐ์ฒด๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ 1
var myArray1 = new Array();
myArray1[0] = "apple";
myArray1[1] = "banana";
myArray1[2] = "orange";

//Array ๊ฐ์ฒด๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ 2
var myArray2 = new Array("apple","banana","orange");

//๋ฆฌํ„ฐ๋Ÿด๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ 
var myArray3 = ["apple", "banana", "orange"];

์ฐธ๊ณ : 2023.01.13 - [1. Revision 2023/Javascript] - [13] 230113 Ch8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ(5)

๋‹ค๋ฅธ ์–ธ์–ด์™€๋Š” ๋‹ค๋ฅธ ์‚ฌํ•ญ

1) ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ž๋™์œผ๋กœ ์กฐ์ ˆ๋œ๋‹ค.
ํ˜„์žฌ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฐ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•œ๋‹ค.

2) ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ž๋ฃŒํ˜•์„ ํ˜ผํ•ฉํ•ด์„œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ex) ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ์ •์ˆ˜์™€ ๋ฌธ์ž์—ด์„ ๋™์‹œ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

var comp = new Array();
comp[0] = "Apple"; //๋ฌธ์ž์—ด
comp[1] = new Date(); //๊ฐ์ฒด (2023-01-03 12:03 ํ˜•ํƒœ๋กœ ๋œฌ๋‹ค)
comp[2] = 3.14; //์ •์ˆ˜

 3) ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฐ ์ธ๋ฑ์Šค ๊ฐ’์œผ๋กœ ๋ฐฐ์—ด ์š”์†Œ์— ์ ‘๊ทผํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  undefined ๊ฐ’์ด ๋ฐ˜ํ™˜๋œ๋‹ค. (ํ”„๋กœ๊ทธ๋žจ์ด ์ค‘์ง€๋˜์ง€ ์•Š๋Š”๋‹ค.)


5.1 Array์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ

length๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์†์„ฑ์œผ๋กœ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.


์˜ˆ์‹œ1) 

for(var i = 0; i<myArray1.length; i++){
document.write(myArray1[i]+" ");
}

for(var i = 0; i<myArray2.length; i++){
document.write(myArray2[i]+" ");
}

for(var i = 0; i<myArray3.length; i++){
document.write(myArray3[i]+" ");
}

๊ฒฐ๊ณผ)


์˜ˆ์‹œ2)

function printArray(a){
document.write("[");
for(var i = 0; i<a.length; i++){
document.write(a[i]+" ");
}
document.write("]");
}

printArray(myArray1);
printArray(myArray2);
printArray(myArray3);

 

๊ฒฐ๊ณผ)


5.2 concat(value1[value2[value...]])

concat() ๋ฉ”์„œ๋“œ๋Š” ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด์˜ ๋์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ธ์ˆ˜๋Š” ๋ฐฐ์—ด์ผ ์ˆ˜๋„ ์žˆ๋‹ค.

์˜ˆ์‹œ)

var x = [1, 2, 3];
var y = [4, 5, 6];
var joined = x.concat(y);
document.write(joined);

๊ฒฐ๊ณผ)


5.3 indexOf(searchStr[, startIndex])

indexOf() ๋ฉ”์„œ๋“œ๋Š” ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
์š”์†Œ(element): ์‹œ์ž‘ ํƒœ๊ทธ + ์ฝ˜ํ…์ธ  + ์ข…๋ฃŒ ํƒœ๊ทธ 

์˜ˆ์‹œ)

var fruits = ["apple", "banana", "grape"];
document.write(fruits.indexOf("banana"));

๊ฒฐ๊ณผ) 
1


5.4 push(value), pop()

push()๋Š” ์Šคํƒ(stack)์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
pop()์€ ์Šคํƒ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
์Šคํƒ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์—ˆ๋‹ค๊ฐ€ ๊บผ๋‚ด๋ฉด ์ˆœ์„œ๊ฐ€ ๋ฐ˜๋Œ€๋กœ ๋œ๋‹ค. (ํ›„์œ„์„ ์ถœ)
(๋ฐ‘์€ ๋ง‰ํ˜€์žˆ๊ณ  ์œ„๋Š” ๋šซํ˜€์žˆ๋‹ค.)
์˜ˆ์‹œ)

var numbers = [1,2,3,4,5];
document.write(numbers+"<br>");

numbers.push(6);
document.write(numbers+"<br>");

numbers.pop();
document.write(numbers+"<br>");


๊ฒฐ๊ณผ)

1,2,3,4,5
1,2,3,4,5,6
1,2,3,4,5

5.5 shift(), unshift()

shift() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ด ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐํ•œ๋‹ค. 

์˜ˆ์‹œ)

var numbers = [1,2,3,4,5,6,7,8,9,10];
var item = numbers.shift();
document.write(item+"<br>");
document.write(numbers+"<br>");


๊ฒฐ๊ณผ)

1
2,3,4,5,6,7,8,9,10


unshift() ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์ชฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘์˜ ํ•˜๋‚˜์ธ ํ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

(์„ ์ž…์„ ์ถœ)


5.6 sort()

sort() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์„ ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

์˜ˆ์‹œ) ์ˆซ์ž์˜ ๊ฒฝ์šฐ ์•ž์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์šฐ์„ ์œผ๋กœ ๋”ฐ์ง„๋‹ค.

var myArray1=['z','a','x','b','c','y'];
var myArray2=[10,7,23,99,169,19,11,1];

myArray1.sort();
document.write(myArray1+"<br>");

myArray2.sort();
document.write(myArray2);

๊ฒฐ๊ณผ) 

a,b,c,x,y,z
1,10,11,169,19,23,7,99

 

์ˆ˜์น˜๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด function (a,b) { return a-b}๋ผ๋Š” ์ •๋ ฌ ๊ธฐ์ค€ ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ œ๊ณตํ•˜๋ฉด ๋œ๋‹ค. 

์˜ˆ์‹œ)

myArray2.sort(function (a,b){return a-b});
document.write(myArray2);


๊ฒฐ๊ณผ)

1,7,10,11,19,23,99,169

5.7 Array.slice([begin[, end]])

slice() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ex) numbers.slice(5, 10) 
๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋„˜๊ธฐ์ง€ ์•Š์œผ๋ฉด, ์‹œ์ž‘ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’๋ถ€ํ„ฐ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๊นŒ์ง€ ๋ชจ๋‘ ๋ณต์‚ฌํ•ด์ค๋‹ˆ๋‹ค.

์‹œ์ž‘ ์ธ๋ฑ์Šค๊ฐ€ ์—†์œผ๋ฉด 0์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค.
์ข…๋ฃŒ ์ธ๋ฑ์Šค๊ฐ€ ์—†์œผ๋ฉด ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ๋ณต์‚ฌํ•œ๋‹ค.

์˜ˆ์‹œ)

var numbers = [1,2,3,4,5,6,7,8,9,10];
var newArray = numbers.slice(5);
document.writeln(newArray+"<br>");

๊ฒฐ๊ณผ)

6,7,8,9,10

5.8 join(delimeter)

join()์€ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅํ•œ๋‹ค.
์ด ๊ธฐ๋Šฅ์€ ๋ฐฐ์—ด์„ ์„œ๋ฒ„๋กœ ๋ณด๋‚ผ ๋•Œ ์•„์ฃผ ์œ ์šฉํ•˜๋‹ค.
์ด๋•Œ ๋ถ„๋ฆฌ์ž๊ฐ€ ๊ฐ ์š”์†Œ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ฒŒ ํ•œ๋‹ค.

์˜ˆ์‹œ) ๋ถ„๋ฆฌ์ž๊ฐ€ + ์ด๋‹ค.

var fruits = ["apple","banana","peach"];
var s = fruits.join("+");
document.write(s);

๊ฒฐ๊ณผ)

apple+banana+peach

5.9 filter()

filter() ๋ฉ”์„œ๋“œ๋Š” ์–ด๋–ค ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์š”์†Œ๋งŒ์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์š”์†Œ๋ฅผ ์„ ํƒํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค. 

์˜ˆ์‹œ)

var numbers = [10,20,30,40,50];
function isBigEnough(element, index, array){
// filter๋ฉ”์„œ๋“œ ์กฐ๊ฑด 
return (element >= 30);
}

var filtered = numbers.filter(isBigEnough);
document.write("ํ•„ํ„ฐ๋ง ๋œ ๋ฐฐ์—ด: "+filtered);

๊ฒฐ๋ก )

ํ•„ํ„ฐ๋ง ๋œ ๋ฐฐ์—ด: 30,40,50

5.10 2์ฐจ์› ๋ฐฐ์—ด p.366


Array ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ Array ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ 2์ฐจ์› ๋ฐฐ์—ด์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

matrix[row(ํ–‰)][column(์—ด)]์ด๋‹ค. 

1 2 3
matrix[0][0] matrix[0][1] matrix[0][2]
4 5 6
matrix[1][0] matrix[1][1] matrix[1][2]
7 8 9
matrix[2][0] matrix[2][1] matrix[2][2]

                                                                                                                             

์˜ˆ์‹œ)

for(let i = 0; i < matrix.length; i++){
    for(let j = 0; j < matrix.length; j++){
     document.write(matrix[i][j]);
     if (j<2){
     document.write(",");
     }
    }
    document.write("<br>");
}

 

๊ฒฐ๊ณผ) 


6. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ p. 367

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์„ ์ €์žฅํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ํ™•๋ฅ ๋„ ๋†’์•„์ง„๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์˜ค๋ฅ˜๋ฅผ ์˜ˆ์™ธ(exception)๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ถ€๋ฅธ๋‹ค. 

(exception: (exceptional event) ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ค‘์— ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ)

์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์›์ธ
- ๊ฐœ๋ฐœ์ž์˜ ํƒ€์ดํ•‘ ์˜ค๋ฅ˜ ๋•Œ๋ฌธ์— ๋ฌธ๋ฒ•์ ์ธ ์˜ค๋ฅ˜
- ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ์ง€์›ํ•˜๋Š” ํŠน์ง•์ด ๋‹ค๋ฅด๋ฏ€๋กœ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž˜๋ชป๋œ ์ž…๋ ฅ
- ์ธํ„ฐ๋„ท ์„œ๋ฒ„ ์˜ค๋ฅ˜ 

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฌด์กฐ๊ฑด ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ”„๋กœ๊ทธ๋žจ์—์„œ ์˜ค๋ฅ˜๋ฅผ ๊ฐ์ง€ํ•˜์—ฌ ์šฐ์•„ํ•˜๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„์— ๊ณ„์† ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋” ๋‚˜์€ ํ”„๋กœ๊ทธ๋žจ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. 
์ด๊ฒƒ์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ(exception handling)๋ผ๊ณ  ํ•œ๋‹ค. 
์ž๋ฐ”์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋™์ผํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค. 

 

6.1 try-Catch ๊ตฌ์กฐ p. 368

์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ธฐ๋Š” try ๋ธ”๋ก๊ณผ catch ๋ธ”๋ก์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.
try ๋ธ”๋ก์—์„œ๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐœ์ƒ๋œ ์˜ˆ์™ธ๋Š” catch ๋ธ”๋ก์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๊ธฐ๋ณธ ํ˜•์‹)

try
{
     // ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
}
catch (๋ณ€์ˆ˜)
{
     // ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
}

์˜ˆ์‹œ)

<script>
var msg = ""
function test(){
try{
allert("Hello");
}catch(error){
msg="๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Œ. " + error.message;
alert(msg);
}
}
</script>
</head>
<body>
<input type = "button" value ="try-catch ์‹œํ—˜" onclick="test()">
</body>

 

๊ฒฐ๊ณผ)


6.2 throw ๋ฌธ์žฅ p. 369

๊ฐœ๋ฐœ์ž๋Š” ์ž์‹ ์ด ์–ด๋–ค ๊ธฐ์ค€์„ ์ •ํ•˜๊ณ  ์ด ๊ธฐ์ค€์— ๋งž์ง€ ์•Š์œผ๋ฉด ์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ค ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
throw ๋ฌธ์žฅ์ด try-catch ๋ฌธ์žฅ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์œผ๋กœ ์˜ˆ์™ธ ๊ฐ์ฒด๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

throw "์˜ˆ์™ธ ๋ฉ”์‹œ์ง€";


์˜ˆ์‹œ) 
1๋ถ€ํ„ฐ 100 ์‚ฌ์ด์— ์žˆ๋Š” ํŠน์ • ์ˆซ์ž๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒŒ์ž„
์ž…๋ ฅ์ด ์—†์„ ๋•Œ, ์ˆซ์ž๊ฐ€ ์•„๋‹ ๋•Œ, ์ˆซ์ž๊ฐ€ ํฌ๊ฑฐ๋‚˜ ์ž‘๊ฑฐ๋‚˜ ์„ฑ๊ณต์ ์ผ ๋•Œ ์˜ˆ์™ธ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ฆ‰์‹œ catch ๋ฌธ์žฅ์— ์˜ํ•˜์—ฌ ์žกํžˆ๊ณ  ์˜ˆ์™ธ์— ํฌํ•จ๋œ ๋ฉ”์‹œ์ง€๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚œ๋‹ค.

- undefined์€ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•˜์ง€ ์•Š์€ ์ƒํƒœ, null์€ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋นˆ ๊ฐ’์„ ํ• ๋‹นํ•œ ์ƒํƒœ(๋นˆ ๊ฐ์ฒด)์ด๋‹ค. ์ฆ‰, undefined๋Š” ์ž๋ฃŒํ˜•์ด ์—†๋Š” ์ƒํƒœ์ด๋‹ค.

var solution = 53;
function test(){
try{
var x = parseInt(document.getElementById("number").value);

/* try catch throw ์‚ฌ์šฉํ•˜๊ธฐ ์ „ 
if (x>solution){
document.getElementById("message").innerHTML="๋„ˆ๋ฌด ํผ";
}*/

if (x == "") throw "์ž…๋ ฅ์ด ์—†์Šต๋‹ˆ๋‹ค."
if (isNaN(x)) throw "์ˆซ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."
if (x > solution) throw "๋„ˆ๋ฌด ํฝ๋‹ˆ๋‹ค."
if (x < solution) throw "๋„ˆ๋ฌด ์ž‘์Šต๋‹ˆ๋‹ค."
if (x == solution) throw "์„ฑ๊ณต์ ์ž…๋‹ˆ๋‹ค." }

catch(error){
var y = document.getElementById("message");
y.innerHTML = "ํžŒํŠธ: " + error; 
}
}
</script>
</head>
<body>
<h1>Number Guess</h1>
<p>1๋ถ€ํ„ฐ 100์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค</p>
<input type = "text" id = "number">
<button type = "button" onclick = "test()">์ˆซ์ž ์ถ”์ธก</button>
<p id = "message"></p>
</body>
</html>

 

๊ฒฐ๊ณผ)


๊ธฐํƒ€

๋ชจ๋“  ๊ฑธ ์•Œ ํ•„์š”๋Š” ์—†๊ณ  ์ž์ฃผ ์“ฐ๋Š” ๊ฑธ ์•Œ๋ฉฐ ๋ชจ๋ฅด๋Š” ๊ฒƒ์€ ๊ฒ€์ƒ‰ํ•˜๊ธฐ by ๊ฐ•์‚ฌ๋‹˜ 


728x90
๋ฐ˜์‘ํ˜•
Comments