μ€ν¬λ¦½νΈλ₯Ό μμ±νλ€ λ³΄λ©΄ μ μ¬ν λμμ νλ μ½λκ° μ¬λ¬ κ³³μμ νμν λκ° λ§λ€.
μ¬μ©μκ° λ‘κ·ΈμΈμ΄λ λ‘κ·Έμμμ νμ λ μλ΄ λ©μμ§λ₯Ό 보μ¬μ£Όλ λμ κ°μ κ²½μ°κ° μλλ°
ν¨μλ νλ‘κ·Έλ¨μ ꡬμ±νλ μ£Όμ 'κ΅¬μ± μμ(building block)'μ΄λ€. ν¨μλ₯Ό μ΄μ©νλ©΄ μ€λ³΅ μμ΄ μ μ¬ν λμμ νλ μ½λλ₯Ό μ¬λ¬ λ² νΈμΆν μ μλ€.
function name(parameter){
ν¨μ λ³Έλ¬Έ
}
ν¨μ μ μΈλ¬Έ : ν¨μλ μ£Όμ μ½λ νλ¦ μ€κ°μ λ
μμ μΈ κ΅¬λ¬Έ ννλ‘ μ‘΄μ¬νλ€.
(μ½λ λΈλ‘μ΄ μ€νλκΈ°λ μ μ μ²λ¦¬λμ΄ λΈλ‘ λ΄ μ΄λμλ νμ© κ°λ₯νλ€.)
function name(a,b){
return a + b;
}
ν¨μ ννμ : ννμμ΄λ ꡬ문 κ΅¬μ± λ΄λΆμ μμ±λ©λλ€.
(μ€ν νλ¦μ΄ ννμμ λ€λ€λμ λ λ§λ€μ΄μ§λ€.)
let sum = function(a,b){
return a + b;
}
function sayhello(nameOfPerson, age){
console.log('Hello my name is' + nameOfPerson + 'and I`m' + age );
} //μ€κ΄νΈ μμ λ€μ΄κ°λ κ²λ€μ΄ μ€νλ²νΌ(sayhello)λ₯Ό λλ₯Ό λλ§λ€ λ°μνλ€.
sayhello('su',10); //Hello my name is su and I`m 10
sayhello('jeong',20);//Hello my name is jeong and I`m 20
sayhello('lee',30);//Hello my name is lee and I`m 30
ν¨μ λ΄μμ μ μΈν λ³μμΈ μ§μ λ³μλ ν¨μ μμμλ§ μ κ·Ό κ°λ₯νλ€.
function showMessage(){
let message = 'hi'//μ§μ λ³μ
alert(message);
}
showMessage(); //hi
alert(message); //error : messageλ ν¨μ λ΄ μ§μ λ³μμ΄κΈ° λλ¬Έμ μλ¬κ° λ°μνλ€.
ν¨μ λ΄λΆμμ ν¨μ μΈλΆμ λ³μμΈ μΈλΆλ³μμ μ κ·Ό κ°λ₯νλ€.
let userName = 'su';
function showMessage(){
let message = 'hi,' + userName//μ§μ λ³μ
alert(message);
}
showMessage(); //hi, su
ν¨μμμ μΈλΆ λ³μ μμ νλ λ°©λ²
let userName = 'su';
function showMessage(){
userName = 'jeong';// 1. μΈλΆλ³μλ₯Ό μμ
let message = 'hi,' + userName
alert(message);
}
alert(userName);//ν¨μ νΈμΆ μ μ΄λ―λ‘ suκ° μΆλ ₯λλ€.
showMessage();
alert(userName);//ν¨μμ μν΄ jeongλ‘ κ°μ΄ λ°λμλ€.
ν¨μλ₯Ό νΈμΆνμ λ ν¨μλ₯Ό νΈμΆν κ³³μ νΉμ κ°μ λ°ννκ² ν μ μλ€.
const age = 96;
function calculateKrAge(ageOfForeigner){
ageOfForeigner + 2;
}
const krAge = calculateKrAge(age);
console.log(krAge);
π undefined
returnμ μ μ©νμ λ
const age = 96;
function calculateKrAge(ageOfForeigner){
return ageOfForeigner + 2; //ν¨μμ 'return'μ μ¬μ©νλ©΄ ν¨μλ₯Ό μ€νν λ
calculateKrAge(age); μ΄ λΆλΆμ λ체νλ€.
}
const krAge = calculateKrAge(age);
console.log(krAge);
π 98
'return'μ νλ©΄ κ³μ°μ λ΄λΉνλ ν¨μμ μ½λλ κ·Έ ν¨μκ° returnνλ κ°κ³Ό κ°μμ§λ€.
const calculator = {
plus : function(a,b){
return a+b;
}
}
const plusResult = calculator.plus(2,3);
console.log(plusResult);
π 5
const plusResult (variable) = calculator.plus(2,3);(return value)
π variableμ ν¨μλ₯Ό ν λΉνλ©΄ μ΄ variableμ ν¨μμ return valueλ₯Ό κ°μ§κ² λλ€
βοΈ νλ² returnνλ©΄ functionμ λλλ€.βοΈ π returnμ νλ©΄, functionμ μλμ λ©μΆκ³ κ²°κ³Ό κ°μ returνκ³ λλλ²λ¦°λ€.
const calculator = {
plus : function(a,b){
console.log("hello"); // μ΄ λΆλΆμ μ μ©λλ€.
return a+b;
console.log("hi"); // returμ μ¬μ©ν΄ μ½λκ° μ’
λ£λμκΈ° λλ¬Έμ μ½λλ μ λ μ μ©
λμ§ μλλ€.
}
}
μΈμ(argument)λ ν¨μλ‘ μ μ λλ μ λ ₯ κ°μ μλ―Ένλλ°, μ΄λ€ κ°μ μΈμλ‘ μ λ¬νλλμ λ°λΌμ ν¨μκ° λ°ννλ κ°μ΄λ λ©μλμ λμ λ°©λ²μ λ€λ₯΄κ² ν μμλ€.
function calculator(arg) {
return arg;
}
alert(calculator(1));// calculator(arg)λ‘ 1μ΄λΌλ κ°μ μ λ¬νκ² λ€λ μλ―Έ.
alert(calculator(2));// calculator(arg)λ‘ 2λΌλ κ°μ μ λ¬νκ² λ€λ μλ―Έ.
π1,2
function calculator(arg1,arg2) {
return arg1 + arg2;
}
alert(calculator(10, 20));// 30
alert(calculator(20, 30));// 50
ν¨μλ return ννΈκ° κ΅μ₯ν 볡μ‘νκ³ μ΄λ ΅μ λ€π’
κ·Έλλ μμ λ₯Ό νμ©ν΄μ κ³μ 곡λΆνλ μ΄μ λ μ‘°κΈ μ΄ν΄κ° λμλλ° κ·Έλλ μμ§ ν·κ°λ¦¬λ건 λ§μ°¬κ°μ§μ΄λ€. μ΄ λΆλΆλ λ°λ³΅ νμ΅λΏμ΄ λ΅μΈ κ² κ°λ€..π₯²