day07
ํจ์์ ์ฌ์ฉ์ ํจ์์ ์ ์(์ ์ธ๊ณผ) ๊ณผ ํจ์์ ํธ์ถ๋ก ๋๋๋ค.
ํจ์์ ์ ์(์ ์ธ)๋ฐฉ๋ฒ
function ํจ์์ด๋ฆ(){ ... ์คํํ ๊ตฌ๋ฌธ.. }ํจ์ ํธ์ถ ๋ฐฉ๋ฒ
ํจ์์ด๋ฆ();
: ํจ์์ ์ ์ธ ๋ฐฉ๋ฒ
<script>
// ํจ์์ ์ ์ธ
function sayHello(){
document.write("<h1>์๋
ํ์ธ์</h1>")
document.write("<h1>์๋ฐ์คํฌ๋ฆฝํธ</h1>")
}
// ํจ์์ ํธ์ถ (์ฌ๋ฌ๋ฒ ์ฌ์ฌ์ฉ ๊ฐ๋ฅ)
sayHello();
sayHello();
sayHello();
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
์๋ฐ์คํฌ๋ฆฝํธ์์๋ ํจ์์ ์ด๋ฆ๋ค์ ์์นํ
๊ดํธ() ์์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ธฐ์ ํ๋ค.function ํจ์์ด๋ฆ(ํ๋ผ๋ฏธํฐ){ .. ์คํํ ๊ตฌ๋ฌธ.. }ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ํจ์ ํธ์ถ
ํจ์์ด๋ฆ(ํ๋ผ๋ฏธํฐ);
: (x) ๊ฐ๋ง ์๋ ํจ์
<script>
function f(x){
var y = x + 1;
document.write("<h1>" + y + "</h1>")
}
f(2); // 2 + 1 = 3
f(3); // 3 + 1 = 4
f(10); // 10 + 1 = 11
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
์๋ฐ์คํฌ๋ฆฝํธ ํจ์์ ๋ค์ค ํ๋ผ๋ฏธํฐ
function ํจ์์ด๋ฆ(ํ๋ผ๋ฏธํฐ1, ํ๋ผ๋ฏธํฐ2){ ... ์คํํ ๊ตฌ๋ฌธ.. }๋ค์ค ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ํจ์ ํธ์ถ
ํจ์์ด๋ฆ(ํ๋ผ๋ฏธํฐ1, ํ๋ผ๋ฏธํฐ2, ...);
<script>
function f(x,y) {
var z = x + y;
document.write("<h1>" + z + "</h1>")
}
f(2 , 1);
f(5, 7);
f(20, 10);
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
๊ฒฐ๊ณผ๊ฐ ๋๋๋ ค ์ฃผ๋ ๊ฐ
function f(x){ return x + 1; }ํธ์ถ
var y = f(5);
<script>
// ํจ์์ ์ธ
function f(x, y){
var z = x + y;
// ์กฐ๊ฑด (10๋ณด๋ค ์์ผ๋ฉด ๋๋ด๊ณ 10๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด ๋ค์ ๋ฆฌํดํจ)
if( z < 10) {
return;
}
return z;
}
// ํจ์์ ํธ์ถ
var a = f(2, 1);
var b = f(5, 7);
var c = f(10, 5);
document.write("<h1>" + a + "</h1>"); // if๋ฌธ์์ ๋๋ฌ๊ธฐ๋๋ฌธ์ ๊ฐ์ด ์ค์ง ์์ undefined๊ฐ ์ถ๋ ฅ
document.write("<h1>" + b + "</h1>");
document.write("<h1>" + c + "</h1>");
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
- ์๋ฐ์คํฌ๋ฆฝํธ ํจ์๋ ์ฒ๋ฆฌ ๋์ค return ๋ฌธ์
๋ง๋๊ฒ ๋๋ฉด ๊ทธ ์ฆ์ ์คํ์ ์ค๋จํ๋ค.- ์ด๋ฌํ ํน์ฑ์ ์ด์ฉํ์ฌ ํน์ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋์ง
์์ ๊ฒฝ์ฐ ๋ฑ์ ๋ํ ์ฒ๋ฆฌ์ค๋จ์ ๋ชฉ์ ์ผ๋ก
return ๋ฌธ์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ฆฌํด๊ฐ ์์ด ์ฒ๋ฆฌ๋ฅผ ์ค๋จํ๊ณ ์ ํ๋ ๊ฒฝ์ฐ์๋ returun ํค์๋๋ง ์ฌ์ฉํ๋ค.- ์ด๋, ๊ฐ์ด ์์ด ๋ฆฌํด ๊ฒฐ๊ณผ๋ฅผ ๋ณ์์ ๋์ ํ ๊ฒฝ์ฐ, ์ ์๋์ง ์์ ๊ฐ์ธ "undefined"๊ฐ ๋์ ๋๋ค.
<script>
function sum(x, y){
var z = x + y;
return z;
}
function printResult(x, y){
var result = sum(x , y);
document.write("<h1>" + result + "</h1>" );
}
printResult(7, 10);
</script>

- ์๋ฐ์คํฌ๋ฆฝํธ์์๋ ์ด๋ค ํจ์ ์์์ ๋ค๋ฅธ ํจ์๋ฅผ ํธ์ถ ํ ์ ์๋ค.
- ํ์ฌ ํจ์ ์์์ ํธ์ถํ ๋ค๋ฅธ ํจ์์ ๋ฆฌํด๊ฐ์
ํ์ฌ ํจ์ ์์์ ๋ค๋ฅธ ๊ณ์ฐ์ ์ฒ๋ฆฌํ๋๋ฐ ํ์ฉํ ์ ์๋ค.
<script>
/*
function numbering(){ .. }
*/
// numbering ์ด๋ผ๋ ๋ณ์๊ฐ ํจ์๋ฅผ ๊ฐ๊ฒ๋๋ค.
let numbering = function(){
let i = 0;
while ( i < 10){
document.write(i);
i += 1;
}
}
numbering();
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
<script>
// ์ต๋ช
ํจ์
// ์ผํ์ฑ ํธ์ถ
(function(){
let i = 0;
while (i<10){
document.write(i);
i +=1;
}
})();
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
<script>
/*
ํ์ดํ ํจ์
- ํจ์ ํํ์๋ณด๋ค ๋จ์ํ๊ณ ๊ฐ๊ฒฐํ ๋ฌธ๋ฒ์ผ๋ก
ํจ์๋ฅผ ๋ง๋ค ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
๋งค๊ฐ๋ณ์๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ
- () => { .... }
: ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๊ฒฝ์ฐ
- x => { ... }
: ๋งค๊ฐ๋ณ์๊ฐ ํ ๊ฐ์ธ ๊ฒฝ์ฐ, ์๊ดํธ
์๋ตํ ์ ์๋ค.
- (x, y ) => {...}
: ๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ ์๊ดํธ
๋ฅผ ์๋ตํ ์ ์๋ค.
ํจ์ ๋ชธ์ฒด ๋ถ๋ถ
- x => { return x * x}
- x => x * x
: ํจ์ ๋ชธ์ฒด๊ฐ ํ์ค์ ๊ตฌ๋ฌธ์ด๋ผ๋ฉด ์ค๊ดํธ๋ฅผ
์๋ตํ ์ ์์ผ๋ฉฐ, ์๋ฌต์ ์ผ๋ก return๋๋ค.
- () = > {
const x = 10;
return x;
}
*/
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๊ฒฝ์ฐ
let foo = () => { console.log('foo function')}
foo();
// ๋งค๊ฐ๋ณ์๊ฐ ํ๋์ธ ๊ฒฝ์ฐ
let foo2 = x => document.write(x);
foo2('user');
let sayHi = () => alert("์๋
ํ์ธ์");
sayHi();
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
๋ค์ ์ถ๋ ฅ๊ฐ
<script>
// ๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ
let user = (a,b) => a + b;
document.write(user(1,3));
document.write('<br>');
let user2 = (a, b) => {return a + b}
document.write(user2(3,4));
document.write('<br>');
let user3 = (a, b) => {return a + b}
document.write(user2(8,4));
document.write('<br>');
let user4 = (a, b) => {
let c = 3;
return a + b + c;
}
document.write(user4(10,11));
document.write('<br/>');
let user5 = n => n*2;
/*
function user5(n){
return n * 2;
}
*/
document/write(user5(3));
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
<script>
let age = prompt("๋์ด๋ฅผ ์๋ ค์ฃผ์ธ์", 18);
let welcome = (age < 18 ) ?
() => alert ('์๋
') :
() => alert ('์๋
ํ์ธ์');
welcome();
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
18์ด๋ณด๋ค ๋์์
18์ด๋ณด๋ค ๋ฎ์์
<script>
function confirmFun(){
if( confirm("ํ์ธ(์) ๋๋ ์ทจ์(์๋์ค)๋ฅผ ์ ํํด ์ฃผ์ธ์") ){
alert("ํ์ธ(์)์ ๋๋ฅด์
จ์ต๋๋ค");
} else {
alert("์ทจ์(์๋์ค)๋ฅผ ๋๋ฅด์
จ์ต๋๋ค");
}
}
confirmFun();
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
์ฒ์ ์ถ๋ ฅ
์ ๋๋ ์์
์ทจ์ ๋๋ ์์
<script>
/*
์ต๋ช
ํจ์
function(){....}
() => {....}
1. ๊ธฐ๋ฅ ๊ตฌํ
2. function() ์ต๋ช
ํจ์
3. () => ์ต๋ช
ํจ์
*/
function ask(question, yes, no){
if( confirm(question)){ // ํ์ธ
yes();
} else { //์ทจ์
no();
}
}
ask('๋์ํ์ญ๋๊น?',
function(){alert('๋์ํ์
จ์ต๋๋ค.')},
function(){alert('์ทจ์ ๋ฒํผ์ ๋๋ฅด์
จ์ต๋๋ค.');});
</script>
๐ก ๊ฒฐ๊ณผ๊ฐ
ํ์ธ ๊ฒฐ๊ณผ
์ทจ์ ๊ฒฐ๊ณผ
<script>
/*
์ต๋ช
ํจ์
function(){....}
() => {....}
1. ๊ธฐ๋ฅ ๊ตฌํ
2. function() ์ต๋ช
ํจ์
3. () => ์ต๋ช
ํจ์
*/
function ask(question, yes, no){
if( confirm(question)){ // ํ์ธ
yes();
} else { //์ทจ์
no();
}
}
ask('๋์ํ์ญ๋๊น?',
() => alert("๋์ํ์
จ์ต๋๋ค."),
() => alert("์ทจ์ ๋ฒํผ์ ๋๋ฅด์
จ์ต๋๋ค."));
</script>
๊ฒฐ๊ณผ๊ฐ์ 12์ ๊ฐ๋ค.