๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

pre-training JavaScript (8) ํ•จ์ˆ˜ , ๊ฐ์ฒด ๋ณธ๋ฌธ

Language/JavaScript

pre-training JavaScript (8) ํ•จ์ˆ˜ , ๊ฐ์ฒด

๊ธฐ๋Œ 2023. 10. 19. 12:48
728x90

๐Ÿ’ป ๊ตญ๋น„ 8 ์ผ์ฐจ


 

๐Ÿ“š ํ•จ์ˆ˜

• ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜  ( ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฌ๋‹ค )

๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜

// ๊ฐ์ฒด๋กœ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ  ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ์ „๋‹ฌ ๋ฐ›์Œ.
function sample(no , name , ban , kor , eng , math , sci , his , tech){
	// ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์€ ์ƒํƒœ
}  

// ๋ถˆํŽธ
sample(39,"๊ธฐ์„ ",2,39,20,50,60,70);

// ๊ฐ์ฒด๋กœ ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›๋Š” ๊ฒฝ์šฐ
function sample(student){

}

// ๊ฐ€๋…์„ฑ์ด ๋›ฐ์–ด๋‚จ
sample({
	no : 39,
    name "๊ธฐ์„ ",
    ban : 17,
    kor : 39,
    eng : 20,
    math : 50,
    sci : 60 , 
    tech : 70
    });

 

// ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜
function check(score){
    let total = score.kor + score.eng + score.math;
    let average = total / 3;

    // 60์  ์ด์ƒ์ด๋ฉด ํ•ฉ๊ฒฉ
    // ๊ทธ์™ธ์—๋Š” ๋ถˆํ•ฉ๊ฒฉ
    if(average >= 60){
        return "ํ•ฉ๊ฒฉ" ;
    }else {
        return "๋ถˆํ•ฉ๊ฒฉ";
    }
}

// ๊ฐ์ฒด ์ž์ฒด๋ฅผ ์ธ์ž,์ธ์ˆ˜๋กœ ๋ฐ›์•„์„œ score1์— ์ €์žฅ
// passed1๋ณ€์ˆ˜๋Š” checkํ•จ์ˆ˜๋ฅผ score1๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ํ˜ธ์ถœํ•ด  return์œผ๋กœ ๋ฐ˜ํ™˜
let score1 = {kor:100 , eng:100 , math:100};
let passed1 = check(score1);
console.log("์ฒซ๋ฒˆ์งธ ์‹œํ—˜ ๊ฒฐ๊ณผ : ",passed1);

// checkํ•จ์ˆ˜์— ์ง์ ‘ ๋ฐ”๋กœ ๊ฐ์ฒด ์ธ์ž๋ฅผ ์ฃผ๊ณ  passed2 ์— ์ €์žฅ
let passed2 = check({kor:80 , eng: 60 ,math:40});
console.log("๋‘๋ฒˆ์งธ ์‹œํ—˜๊ฒฐ๊ณผ : ",passed2);

// ์œ„์— ๊ฒƒ๊ณผ ๋‹ค๋ฅธ์ ์€ ์ค„๋ฐ”๊ฟˆ ๋ฐ–์— ์—†์Œ
// ๊ฐ€๋…์„ฑ ๋ณด๊ธฐ ์ข‹์œผ๋ผ๊ณ  ์ค„๋ฐ”๊ฟˆ ์‚ฌ์šฉํ•จ
let passed3 = check({
    kor : 70,
    eng : 90,
    math :60
})
console.log("์„ธ๋ฒˆ์งธ ์‹œํ—˜๊ฒฐ๊ณผ : ",passed3);

• ์ธ์ž๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ธฐ๋Šฅ๋„ ์ „๋‹ฌํ•ด์ค„์ˆ˜ ์žˆ๋‹ค.

  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜
function plus(num1,num2){
    let result = num1 + num2;
    console.log("๋ง์…ˆ๊ฒฐ๊ณผ : ", result);
    console.log();
}

function minus(num1,num2){
    let result = num1 - num2;
    console.log("๋บ„์…ˆ ๊ฒฐ๊ณผ : ", result);
    console.log();
}

// ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜
function operator(value1 , value2 , fn){
    fn(value1, value2);
}


operator(100 , 200 , plus);
operator(100 , 200 , minus);
operator(100,200,function(x,y){
    let result = x * y ;
    console.log(
        "๊ณฑ์…ˆ๊ฒฐ๊ณผ : ",result
    )}
)

function divide(num1,num2){
    let result = num1 / num2;
    console.log("๋‚˜๋ˆ„๊ธฐ ๊ฒฐ๊ณผ :", result);
}

operator(100,10,divide);
  • ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ•œ๋‹ค
  • ๊ทธ ํ›„์— ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•˜๋Š” ๋ฐฉ์‹

๐Ÿ“Œ ๋™์ž‘ ๋ฐฉ๋ฒ•

  • operator( 100 , 10 , divide);  operator ํ•จ์ˆ˜๋ฅผ 100 , 10 , divide ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•ด์„œ ํ˜ธ์ถœํ•œ๋‹ค
  • operator ํ•จ์ˆ˜ ์•ˆ์—์„œ fn(value1 , value2) ๊ฐ€ ์‹คํ–‰์ด ๋˜๋Š”๋ฐ fn์€ divide ํ•จ์ˆ˜ 
  • divide(100,10)๊ฐ€ ํ˜ธ์ถœ
  • divide ํ•จ์ˆ˜ ์•ˆ์— 100์„ 10 ์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒฐ๊ณผ 10 ์ด result ๋ณ€์ˆ˜์— ์ €์žฅ์ด๋จ
  • ๋‚˜๋ˆ„๊ธฐ ๊ฒฐ๊ณผ : 10 ์ด ์ถœ๋ ฅ๋œ๋‹ค

๐Ÿ“š ๊ฐ์ฒด 

• ๋‚ ์งœ ๊ฐ์ฒด

์†์„ฑ : ์‹œ๊ฐ„๋Œ€ , ๋…„๋„ , ์›” , ์ผ , ์‹œ , ๋ถ„ , ์ดˆ , ์˜ค์ „/์˜คํ›„ . ์š”์ผ..
๊ธฐ๋Šฅ : ๋‚ ์งœ/์‹œ๊ฐ„ ๋ณ€๊ฒฝ , ์ถœ๋ ฅ๊ธฐ๋Šฅ

• ์„ฑ์  ๊ฐ์ฒด

์†์„ฑ: ์ด๋ฆ„ , ๊ณผ๋ชฉ๋ช… , ๊ตญ์–ด , ์ˆ˜ํ•™ , ์˜์–ด
๊ธฐ๋Šฅ : ์ถœ๋ ฅ๊ธฐ๋Šฅ , ์ด ์ ์ˆ˜ ์กฐํšŒ , ํ‰๊ท  ์ ์ˆ˜ ์กฐํšŒ

// ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด

let score1 = {
    // ์†์„ฑ property
    name : "๊ธฐ์„ ",
    kor : 67,
    eng : 55,
    math : 60,

    // ๊ธฐ๋Šฅ method
    getTotal: function(){
        let total = this.kor + this.eng + this.math;
        return total;
    },
    getAverage : function(){
        let average =this.getTotal() / 3;
        return average;
    },
    print : function(){
        console.log("--- ์„ฑ์  ์ •๋ณด ---");
        console.log("์ด๋ฆ„ : ",this.name);
        console.log("๊ตญ์–ด : ",this.kor);
        console.log("์˜์–ด : ",this.eng);
        console.log("์ˆ˜ํ•™ : ",this.math);
        console.log("์ด์  : ",this.getTotal());
        console.log("ํ‰๊ท  : ",this.getAverage());
    }
}

// print()๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์˜ ์ฃผ์†Œ ๊ฐ’ ?
score1.print();


// console.log(score1);


// let total1 = score1.getTotal();
// console.log("์ด์  : ",total1);

// let average1 = score1.getAverage();
// console.log("ํ‰๊ท  : ", average1);

• ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ

// ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
let score = {
	name : "๊ธฐ์„ ",
    kor :100 ,
    eng : 30,
    math : 50
    getTotal:function(){
    	let total = this.kor + this.eng + this.math;
        return total;
        }
    }

โœ” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๊ผญ ์ฒซ๊ธ€์ž ๋Œ€๋ฌธ์ž๋กœ !

// ํ‹€๋ฆฐ๋ฒ„์ „ 
Score("๊ธฐ์„ " , 27 , 90 , 93);

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ new ๋ผ๋Š” ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ
new Score("๊ธฐ์„ " , 27 , 90 , 93);
function Score(name , kor , eng , math){
    // property ์ƒ์„ฑ , ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’์„ property์— ๋Œ€์ž…
    this.name = name;
    this.kor = kor;
    this.eng = eng;
    this.math = math;

    this.getTotal = function() {
        let total = this.kor + this.eng + this.math;
        return total;
    }

    this.getAverage = function(){
        let average = this.getAverage()/3
        return average;
    }

	    this.print = function(){
        console.log("--------์„ฑ์ ์ •๋ณด---------")
        console.log("์ด๋ฆ„ : ",this.name);
        console.log("๊ตญ์–ด : ",this.kor);
        console.log("์˜์–ด : ",this.eng);
        console.log("์ˆ˜ํ•™ : ",this.math);
        console.log("์ด์  : ",this.getTotal());
        console.log("ํ‰๊ท  : ",this.getAverage());
        console.log();
    }

}

let sc = new Score("๊ธฐ์„ " , 27 , 90 , 93);
let s1 = new Score("๊น€์œ ์‹ " , 100 , 100 , 20 );
let s2 = new Score("๊น€์‹œ๋ฏผ" , 30 , 60 , 100 );
let s3 = new Score("ํ•œ๋ช…ํšŒ" , 90 , 100 , 90 );

console.log(s1);
console.log(s2);
console.log(s3);

sc.print();
s1.print();
s2.print();
s3.print();


๐Ÿ“Œ  window ( ์ฐธ์กฐ๋ณ€์ˆ˜ ) 

Window ๊ฐ์ฒด ( ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ๊ธ€๋กœ๋ฒŒ ๊ฐ์ฒด )

  • console ← Console ๊ฐ์ฒด ( ํ™”๋ฉด ์ถœ๋ ฅ๊ณผ ๊ด€๋ จ๋œ ๊ฐ์ฒด )
  • document ← Document  ๊ฐ์ฒด ( ์šฐ๋ฆฌ๊ฐ€ ์›นํŽ˜์ด์ง€์—์„œ ๋ณด๋Š” ๋ฌธ์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ , ์›น html) 
  • location ← Location ๊ฐ์ฒด ( ์›น ๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œ์ฐฝ์— ๋ณด์ด๋Š” ํ˜„์žฌ ํ‘œ์‹œ๋œ ์ฃผ์†Œ ) 
  • history ← History ๊ฐ์ฒด ( ์›น ํŽ˜์ด์ง€ ๋ฐฉ๋ฌธ ์ด๋ ฅ์ •๋ณด )  // history.back()   history.forward()

โ–ถ window.console.log() ์ธ๋ฐ  window๋ฅผ ์ ์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ธ€๋กœ๋ฒŒ ๊ฐ์ฒด์—์„œ ์ฐพ์€๋’ค
์ƒ๋žต ํ•œ ๋’ค์— console.log()๋ฅผ ์‚ฌ์šฉ ํ•œ๋‹ค 

 

 

 

728x90

'Language > JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

01. JavaScript  (0) 2024.01.12
pre-training JavaScript (9)  (1) 2023.10.20
pre-training JavaScript (7) ํ•จ์ˆ˜  (1) 2023.10.18
pre-training JavaScript (6) ๋ฐฐ์—ด , ๊ฐ์ฒด (์˜ค๋ธŒ์ ํŠธ)  (1) 2023.10.17
pre-training JavaScript (5) ๋ฐ˜๋ณต๋ฌธ  (0) 2023.10.16