ν°μ€ν 리 λ·°
κ°μ²΄ μμ±μ ν¨μ | 리ν°λ΄ ν¨μ | νλ‘νΈνμ ν¨μ - ν¨μ μ ν
κΉμ½λ¦°π©π»π» 2022. 8. 22. 12:22ν¨μ μ ν (Function Type)
ν¨μλ νλ‘κ·Έλ¨μμ λ°λ³΅μ μΌλ‘ μ¬μ©λλ κΈ°λ₯μ λ§λ€μ΄ λ΄κΈ° μν μ½λλ€μ μ§ν©
1. ν¨μ μ ν : ν¨μμ λ§€κ°λ³μλ₯Ό μ΄μ©ν νν
μ½λλ₯Ό μ¬νμ©νκΈ° μν΄ ν¨μλ₯Ό μ¬μ©νλ€.
function func(num, str1, str2){
document.write(num+ ". " + str1 + "κ°" + str2 + "λμμ΅λλ€. " + "
")
}
func("1","ν¨μ", "μ€ν") //1. ν¨μκ° μ€νλμμ΅λλ€.
func("2","μλ°μ€ν¬λ¦½νΈ", "μ€ν") //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
func("3","μ μ΄μΏΌλ¦¬", "μ€ν") //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
2. ν¨μ μ ν : ν¨μμ λ³μλ₯Ό μ΄μ©ν νν
function func(num, str1, str2){
document.write(num+ ". " + str1 + "κ°" + str2 + "λμμ΅λλ€. " + "
")
}
const youNum1 = 1;
const youNum2 = 2;
const youNum3 = 3;
const youStr1 = "ν¨μ";
const youStr2 = "μλ°μ€ν¬λ¦½νΈ";
const youStr3 = "μ μ΄μΏΌλ¦¬";
const youCom = "μ€ν";
func(youNum1, youStr1, youCom); //1. ν¨μκ° μ€νλμμ΅λλ€.
func(youNum2, youStr2, youCom); //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
func(youNum3, youStr3, youCom); //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
3. ν¨μ μ ν : ν¨μμ λ°°μ΄, κ°μ²΄λ₯Ό μ΄μ©ν νν
function func(num, str1, str2){
document.write(num+ ". " + str1 + "κ°" + str2 + "λμμ΅λλ€. " + "
")
}
const info = [
{
num : "1",
name : "ν¨μ",
com : "μ€ν"
},
{
num : "2",
name : "μλ°μ€ν¬λ¦½νΈ",
com : "μ€ν"
},
{
num : "3",
name : "μ μ΄μΏΌλ¦¬",
com : "μ€ν"
}
]
func(info[0].num, info[0].name, info[0].com) //1. ν¨μκ° μ€νλμμ΅λλ€.
func(info[1].num, info[1].name, info[1].com) //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
func(info[2].num, info[2].name, info[2].com) //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
4. ν¨μ μ ν : κ°μ²΄ μμ λ³μμ ν¨μλ₯Ό μ΄μ©ν νν
const info = {
num1 : 1,
name1 : "ν¨μ",
word1 : "μ€ν",
num2 : 2,
name2 : "μλ°μ€ν¬λ¦½νΈ",
word2 : "μ€ν",
num3 : 3,
name3 : "μ μ΄μΏΌλ¦¬",
word3 : "μ€ν",
result1 : function(){
document.write(info.num1 + ". "+ info.name1 +"κ°" + info.word1 +"λμμ΅λλ€.
");
},
result2 : function(){
document.write(info.num2 + ". "+ info.name2 +"κ°" + info.word2 +"λμμ΅λλ€.
");
},
result3 : function(){
document.write(info.num3 + ". "+ info.name3 +"κ°" + info.word3 +"λμμ΅λλ€.
");
},
}
info.result1(); //1. ν¨μκ° μ€νλμμ΅λλ€.
info.result2(); //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
info.result3(); //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
5. ν¨μ μ ν : κ°μ²΄ μμ±μ ν¨μ*****
κΈ°μ‘΄ ν¨μμ new μ°μ°μλ₯Ό λΆμ¬μ νΈμΆνλ©΄, ν΄λΉ ν¨μλ μμ±μ ν¨μλ‘ λμν©λλ€.
리ν°λ΄κ³Ό Obejct()λ‘ κ°μ²΄λ₯Ό μμ±νλ κ²κ³Ό λ¬λ¦¬, μμ±μ ν¨μλ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νλ©΄ κ°μ μμ±μ κ°μ§ κ°μ²΄λ₯Ό μ¬λ¬ κ° μμ±ν μ μμ΅λλ€.
μμ±μ ν¨μλ‘ μΈμ€ν΄μ€λ₯Ό μμ±νκΈ° μ μ, λ¨Όμ λΉμ΄μλ κ°μ²΄λ₯Ό μμ±ν©λλ€.
function func(num, name, word){
this.num = num; //constλ₯Ό μλ΅νκ³ , thisλ₯Ό μ¬μ©ν¨μΌλ‘μ¨ λ³μ μ¬νμ©κ°λ₯
this.name = name;
this.word = word;
this.result = function(){
document.write(this.num + ". "+ this.name +"κ°" + this.word +"λμμ΅λλ€.
");
}
}
//μΈμ€ν΄νΈ μμ±
const info1 = new func("1","ν¨μ","μ€ν")
const info2 = new func("2","μλ°μ€ν¬λ¦½νΈ","μ€ν")
const info3 = new func("3","μ μ΄μΏΌλ¦¬","μ€ν")
//μ€ν
info1.result(); //1. ν¨μκ° μ€νλμμ΅λλ€.
info2.result(); //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
info3.result(); //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
6. ν¨μ μ ν : νλ‘νΈνμ ν¨μ
ν¨μ νλ‘ν νμ
μ EGL μμ€ν
μ½λκ° ν¨μ μ체μ μ‘μΈμ€νμ§ λͺ»νλ κ²½μ° ν¨μ νΈμΆμ λν΄ μ ν κ²μ¬λ₯Ό μννλ λ° μ¬μ©λλ μ μμ
λλ€.
ν¨μ νλ‘ν νμ
μ function ν€μλλ‘ μμν λ€μ ν¨μ μ΄λ¦, ν΄λΉ λ§€κ°λ³μ(μλ κ²½μ°), 리ν΄κ°(μλ κ²½μ°)μ λμ΄ν©λλ€.
function func(num, name, word){
this.num = num; //constλ₯Ό μλ΅νκ³ , thisλ₯Ό μ¬μ©ν¨μΌλ‘μ¨ λ³μ μ¬νμ©κ°λ₯
this.name = name;
this.word = word;
}
//resultλ₯Ό ν¨μ λ°μΌλ‘ λΉΌμ€
func.prototype.result = function(){
document.write(this.num + ". "+ this.name +"κ°" + this.word +"λμμ΅λλ€.
");
}
//μΈμ€ν΄νΈ μμ±
const info1 = new func("1","ν¨μ","μ€ν")
const info2 = new func("2","μλ°μ€ν¬λ¦½νΈ","μ€ν")
const info3 = new func("3","μ μ΄μΏΌλ¦¬","μ€ν")
//μ€ν
info1.result(); //1. ν¨μκ° μ€νλμμ΅λλ€.
info2.result(); //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
info3.result(); //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
7. ν¨μ μ ν : κ°μ²΄ 리ν°λ΄ ν¨μ
μλ°μ€ν¬λ¦½νΈ ν¨μλ μλ³μ μμ΄ ν¨μ κ·Έ μ체λ₯Ό κ°μ²΄ νμ
μ κ°μΌλ‘ 리ν°λ΄λ‘ μμ±μ΄ κ°λ₯ν©λλ€.
κ°μ²΄ νμ
μ κ°μΌλ‘ μμ±λ ν¨μ 리ν°λ΄μ λ νΌλ°μ€λ₯Ό λ³μμ λμ
ν΄μ€μΌλ‘μ¨ , λ³μλͺ
(μΈμλͺ©λ‘) μ λ°©λ²μΌλ‘λ νΈμΆμ΄ κ°λ₯νκ³ , νλ²λ§ νΈμΆνκ³ λ€μ νΈμΆν μ μλ μ¦μ μ€ν ν¨μ (ν¨μ 리ν°λ΄)μΌλ‘λ νΈμΆν μ μμ΅λλ€.
function func(num, name, word){
this.num = num; //constλ₯Ό μλ΅νκ³ , thisλ₯Ό μ¬μ©ν¨μΌλ‘μ¨ λ³μ μ¬νμ©κ°λ₯
this.name = name;
this.word = word;
}
//κ°μ²΄ 리ν°λ΄ ν¨μ
func.prototype = {
result1 : function(){
document.write(this.num + ". "+ this.name +"κ°" + this.word +"λμμ΅λλ€.
");
},
result2 : function(){
document.write(this.num + ". "+ this.name +"κ°" + this.word +"λμμ΅λλ€.
");
},
result3 : function(){
document.write(this.num + ". "+ this.name +"κ°" + this.word +"λμμ΅λλ€.
");
}
}
//μΈμ€ν΄νΈ μμ±
const info1 = new func("1","ν¨μ","μ€ν")
const info2 = new func("2","μλ°μ€ν¬λ¦½νΈ","μ€ν")
const info3 = new func("3","μ μ΄μΏΌλ¦¬","μ€ν")
//μ€ν
info1.result1(); //1. ν¨μκ° μ€νλμμ΅λλ€.
info2.result2(); //2. μλ°μ€ν¬λ¦½νΈκ° μ€νλμμ΅λλ€.
info3.result3(); //3. μ μ΄μΏΌλ¦¬κ° μ€νλμμ΅λλ€.
'Javascript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
search( ) (1) | 2022.08.22 |
---|---|
search( ) (1) | 2022.08.22 |
includes() (2) | 2022.08.17 |
padStart() | padEnd() (1) | 2022.08.17 |
repeat() (2) | 2022.08.17 |