๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

ํ•จ์ˆ˜ ์ดํ•ดํ•˜๊ธฐ 2

by Youcodein 2022. 8. 22.
728x90
๋ฐ˜์‘ํ˜•

ํ•จ์ˆ˜์˜ ์œ ํ˜• : ํ•จ์ˆ˜์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•œ ํ˜•ํƒœ

function func(num, str1, str2) {
    document.write(num + str1 + str2 + "๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
}
func("1", "ํ•จ์ˆ˜", "์‹คํ–‰");
func("2", "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ", "์‹คํ–‰");
func("3", "์ œ์ด์ฟผ๋ฆฌ", "์‹คํ–‰");
๊ฒฐ๊ณผ ๋ณด๊ธฐ

ํ•จ์ˆ˜ ์œ ํ˜• : ํ•จ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•œ ํ˜•ํƒœ

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);
func(youNum2, youStr2, youCom);
func(youNum3, youStr3, youCom);
// 1. ํ•จ์ˆ˜๊ฐ€์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
// 2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
// 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);
func(info[1].num, info[1].name, info[1].com);
func(info[2].num, info[2].name, info[2].com);
๊ฒฐ๊ณผ ๋ณด๊ธฐ

ํ•จ์ˆ˜ ์œ ํ˜• : ๊ฐ์ฒด ์•ˆ์— ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ํ˜•ํƒœ

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();
info.result2();
info.result3();
๊ฒฐ๊ณผ ๋ณด๊ธฐ

ํ•จ์ˆ˜ ์œ ํ˜• : ๊ฐ์ฒด ์ƒ์„ฑ์ž ํ•จ์ˆ˜

function func(num, name, word) {
      this.num = num;
      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();
  info2.result();
  info3.result();
info.result3();
๊ฒฐ๊ณผ ๋ณด๊ธฐ

ํ•จ์ˆ˜ ์œ ํ˜• : ํ”„๋กœํ† ํƒ€์ž… ํ•จ์ˆ˜

function func(num, name, word){
    this.num = num;
    this.name = name;
    this.word = word;
}

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();
info2.result();
info3.result();
๊ฒฐ๊ณผ ๋ณด๊ธฐ
1. ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3. ์ œ์ด์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜ ์œ ํ˜• : ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ•จ์ˆ˜

function func(num, name, word) {
    this.num = num;
    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();
info2.result2();
info3.result3();
๊ฒฐ๊ณผ ๋ณด๊ธฐ
1. ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3. ์ œ์ด์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•

'์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JQuery๋ž€?  (5) 2022.08.30
GSAP๋ž€?  (7) 2022.08.29
charAt()  (7) 2022.08.22
match()  (6) 2022.08.22
search()  (5) 2022.08.22

๋Œ“๊ธ€