λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
JAVASCRIPT

[JAVASCRIPT] ν•¨μˆ˜μ˜ μœ ν˜•

by _토맀토 2022. 8. 23.
728x90

μžλ°”μŠ€ν¬λ¦½νŠΈ : ν•¨μˆ˜μ˜ μœ ν˜•

이번 μ‹œκ°„μ—λŠ” ν•¨μˆ˜μ˜ μœ ν˜•μ— λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€! 😁


#1. ν•¨μˆ˜ μœ ν˜• : ν•¨μˆ˜μ™€ λ³€μˆ˜λ₯Ό μ΄μš©ν•œ ν˜•νƒœ

첫번째 μœ ν˜•μ€ λ³€μˆ˜λ₯Ό 톡해 데이터λ₯Ό μ „λ‹¬μ‹œν‚΅λ‹ˆλ‹€.

! μ‚¬μš© 방법 !
각 λ³€μˆ˜λ§ˆλ‹€ 데이터 값을 μ„ μ–Έν•˜κ³  ν•¨μˆ˜μ— λ³€μˆ˜λ₯Ό μ €μž₯ν•˜μ—¬ 싀행문을 톡해 데이터λ₯Ό 전달!

function func(num, str1, str2){
    document.write(num + ". " + str1 + "κ°€ " + str2 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.14");
}
const youNum1 = 1;
const youNum2 = 2;
const youNum3 = 3;
const youStr1 = "ν•¨μˆ˜";
const youStr2 = "μžλ°”μŠ€ν¬λ¦½νŠΈ";
const youStr3 = "제이쿼리";
const youCom1 = "μ‹€ν–‰";

func(youNum1, youStr1, youCom1);
func(youNum2, youStr2, youCom1);
func(youNum3, youStr3, youCom1);
결과 보기
1. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.14 2. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.14 3. μ œμ΄μΏΌλ¦¬κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.14

#2. ν•¨μˆ˜ μœ ν˜• : ν•¨μˆ˜μ™€ λ°°μ—΄ , 객체λ₯Ό μ΄μš©ν•œ ν˜•νƒœ

λ‘λ²ˆμ§Έ μœ ν˜•μ€ 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ 데이터λ₯Ό μ „λ‹¬μ‹œν‚΅λ‹ˆλ‹€.

function func(num, str1, str2){
    document.write(num + ". " + str1 + "κ°€ " + str2 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.15");
}

const info = [
    {
        num :  "1",
        name :  "ν•¨μˆ˜",
        com :  "μ‹€ν–‰"
    },
    {
        num :  "2",
        name :  "μžλ°”μŠ€νŠΈλ¦½νŠΈ",
        com :  "μ‹€ν–‰"
    },
    {
        num :  "3",
        name :  "제이쿼리",
        com :  "μ‹€ν–‰"
    }
];
func(info[0].num, info[0].name, info[0].com);
func(info[1].num, info[1].name, info[1].com);
func(info[2].num, info[2].name, info[2].com);
결과 보기
1. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.15 2. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.15 3. μ œμ΄μΏΌλ¦¬κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.15

#3. ν•¨μˆ˜ μœ ν˜• : 객체 μƒμ„±μž ν•¨μˆ˜

객체가 ν•˜λ‚˜κ°€ μžˆμ„ 땐 info 뒀에 []λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
16번 μœ ν˜•μ€ μž¬μ‚¬μš©μ΄ λΆˆκ°€ν•˜κΈ° λ•Œλ¬Έμ— μ•„λž˜μ˜ 17번 식이 λ§Œλ“€μ–΄μ§€κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

const info = {
    num1 : 1,
    name1 : "ν•¨μˆ˜",
    word1 : "μ‹€ν–‰",
    num2 : 2,
    name2 : "μžλ°”μŠ€ν¬λ¦½νŠΈ",
    word2 : "μ‹€ν–‰",
    num3 : 3,
    name3 : "제이쿼리",
    word3 : "μ‹€ν–‰",

    result1 : function(){    
        //객체 μ•ˆμ— ν•¨μˆ˜λ₯Ό 넣은 μƒνƒœ 
        document.write(info.num1 + ". " + info.name1 + "κ°€ " + info.word1 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.16");
    },
    result2 : function(){    
        //객체 μ•ˆμ— ν•¨μˆ˜λ₯Ό 넣은 μƒνƒœ 
        document.write(info.num2 + ". " + info.name2 + "κ°€ " + info.word2 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.16");
    },
    result3 : function(){    
        //객체 μ•ˆμ— ν•¨μˆ˜λ₯Ό 넣은 μƒνƒœ 
        document.write(info.num3 + ". " + info.name3 + "κ°€ " + info.word3 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.16");
    }
}
info.result1(); 
info.result2(); 
info.result3(); 
κ²°κ³Ό 보기 1. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.16 2. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.16 3. μ œμ΄μΏΌλ¦¬κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.16

#4. ν•¨μˆ˜ μœ ν˜• : 객체 μƒμ„±μž ν•¨μˆ˜

λ„€λ²ˆμ§Έ μœ ν˜•μ€ 객체 μƒμ„±μž ν•¨μˆ˜λ‘œ λ‘λ²ˆμ§Έ μœ ν˜•μ˜ 단점인 μž¬μ‚¬μš©μ„ μœ„ν•΄ μƒˆλ‘œ λ§Œλ“€μ–΄μ§„ 객체 μƒμ„±μž ν•¨μˆ˜μž…λ‹ˆλ‹€.

! 객체 μƒμ„±μž ν•¨μˆ˜μ˜ νŠΉμ§• !
1. this. : 이것을 μ €μž₯ν•˜μ‹œμ˜€. λΌλŠ” 의미둜 μž¬μ‚¬μš©μ„ κ°€λŠ₯ν•˜κ²Œ ν•΄μ€€λ‹€. 2. 객체 μƒμ„±μžλ₯Ό 좜λ ₯ν•˜λ €λ©΄ κΌ­ μΈμŠ€ν„΄νŠΈλ₯Ό 생성 ν•œλ’€ 싀행문을 써주어야 ν•œλ‹€.

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 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.17
"); } } //μΈμŠ€ν„΄νŠΈ 생성 - 데이터 μ €μž₯ const info1 = new func("1", "ν•¨μˆ˜", "μ‹€ν–‰"); //데이터 μ €μž₯ : this. : 이것을 μ €μž₯ν•˜μ‹œμ˜€. λΌλŠ” 의미! const info2 = new func("2", "μžλ°”μŠ€ν¬λ¦½νŠΈ", "μ‹€ν–‰"); const info3 = new func("3", "제이쿼리", "μ‹€ν–‰"); //μ‹€ν–‰ info1.result(); info2.result(); info3.result();
κ²°κ³Ό 보기 1. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.17 2. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.17 3. μ œμ΄μΏΌλ¦¬κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.17

#5. ν•¨μˆ˜ μœ ν˜• : ν”„λ‘œν† νƒ€μž… ν•¨μˆ˜

ν”„λ‘œν† νƒ€μž„ ν•¨μˆ˜λŠ” 데이터에 이어 λ©”λͺ¨λ¦¬κΉŒμ§€ μž¬μ‚¬μš©μ„ ν•˜κΈ° μœ„ν•΄ λ‚˜μ˜¨ ν•¨μˆ˜μž…λ‹ˆλ‹€.

unction func(num, name, word){ //μ €μž₯ν•œ 데이터λ₯Ό λ³€μˆ˜μ—κ²Œ 데이터 전달
    this.num = num; //μž¬μ‚¬μš©μšΈ μœ„ν•΄ constλ₯Ό μƒλž΅ν•˜κ³  this.λ₯Ό 뢙인닀.
    this.name = name;
    this.word = word;
}
func.prototype.result = function(){
    document.write(this.num + ". " + this.name + "κ°€ " + this.word + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.18
"); } //μΈμŠ€ν„΄νŠΈ 생성 - 데이터 μ €μž₯ const info1 = new func("1", "ν•¨μˆ˜", "μ‹€ν–‰"); //데이터 μ €μž₯ : this. : 이것을 μ €μž₯ν•˜μ‹œμ˜€. λΌλŠ” 의미! const info2 = new func("2", "μžλ°”μŠ€ν¬λ¦½νŠΈ", "μ‹€ν–‰"); const info3 = new func("3", "제이쿼리", "μ‹€ν–‰"); //μ‹€ν–‰ info1.result(); info2.result(); info3.result();
κ²°κ³Ό 보기 1. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.18 2. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.18 3. μ œμ΄μΏΌλ¦¬κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.18

#6. ν•¨μˆ˜ μœ ν˜• : 객체 λ¦¬ν„°λŸ΄ ν•¨μˆ˜

λ§ˆμ§€λ§‰ μœ ν˜•μΈ 객체 λ¦¬ν„°λŸ΄ ν•¨μˆ˜λŠ” ν•¨μˆ˜λ₯Ό λ¦¬ν„°λŸ΄ 객체 μ•ˆμ— λ„£μ–΄ μ›ν•˜λŠ” κ°’λ§Œ μΆ”μΆœν•˜κΈ° μš©μ΄ν•©λ‹ˆλ‹€.

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 + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.19
"); }, result2 : function(){ document.write(this.num + ". " + this.name + "κ°€ " + this.word + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.19
"); }, result3 : function(){ document.write(this.num + ". " + this.name + "κ°€ " + this.word + "λ˜μ—ˆμŠ΅λ‹ˆλ‹€.19
"); } } //μΈμŠ€ν„΄νŠΈ 생성 - 데이터 μ €μž₯ const info1 = new func("1", "ν•¨μˆ˜", "μ‹€ν–‰"); //데이터 μ €μž₯ : this. : 이것을 μ €μž₯ν•˜μ‹œμ˜€. λΌλŠ” 의미! const info2 = new func("2", "μžλ°”μŠ€ν¬λ¦½νŠΈ", "μ‹€ν–‰"); const info3 = new func("3", "제이쿼리", "μ‹€ν–‰"); //μ‹€ν–‰ info1.result1(); info2.result2(); info3.result3();
κ²°κ³Ό 보기 1. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.19 2. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.19 3. μ œμ΄μΏΌλ¦¬κ°€ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.19
728x90

λŒ“κΈ€


Lucky Charms Rainbow
js
html
css