๐Ÿš€ Languege/JavaScript

[๊ธฐ๋ณธ ๋ฌธ๋ฒ•] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜ (javascript function)

mini_world 2021. 4. 15. 01:06
๋ชฉ์ฐจ ์ ‘๊ธฐ

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ ๊ตฌ์„ฑ ๋ธ”๋ก์ค‘ ํ•˜๋‚˜๋กœ, ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฑฐ๋‚˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ์žฌ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฌธ์žฅ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.
ํ•จ์ˆ˜๋„ ์ผ๋ฐ˜ ๊ฐ์ฒด(๋ฐ์ดํ„ฐ ํƒ€์ž…)๋กœ, ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ์ž‘์„ฑํ–ˆ๋˜ Object์™€ ๊ฐ™์€ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด, ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ• ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” console.log๋„ console ๊ฐ์ฒด์— ๋“ค์–ด์žˆ๋Š” log ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

console.log(console)

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
Object [console] {
  log: [Function: log],
  warn: [Function: warn],
  dir: [Function: dir],
  time: [Function: time],
  timeEnd: [Function: timeEnd],
  timeLog: [Function: timeLog],
  trace: [Function: trace],
  assert: [Function: assert],
  clear: [Function: clear],
  count: [Function: count],
  countReset: [Function: countReset],
  group: [Function: group],
  groupEnd: [Function: groupEnd],
  table: [Function: table],
  debug: [Function: debug],
  info: [Function: info],
  dirxml: [Function: dirxml],
  error: [Function: error],
  groupCollapsed: [Function: groupCollapsed],
  Console: [Function: Console],
  profile: [Function: profile],
  profileEnd: [Function: profileEnd],
  timeStamp: [Function: timeStamp],
  context: [Function: context]
}
-----------------------------------
*/

console.log์™€ ๊ฐ™์€ ๊ฐ์ฒดํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค.


1. Basic function ๋งŒ๋“ค๊ธฐ 

ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ• ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ๋ฒ•์„ ์ง€์ผœ์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.
์•„๋ž˜ ์˜ˆ์ œ๋Š” Hello World! ๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

function ํ•จ์ˆ˜๋ช… (์ธ์ž๊ฐ’) { 
          ์‹คํ–‰๋ฌธ;
}
function sampleFunc() {
    console.log('hello world!');
}

sampleFunc();

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ 
// hello world!

 

2. ์ธ์ž(argument) ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” function ๋งŒ๋“ค๊ธฐ

์ธ์ž(argument)๋Š” ํŒŒ๋ผ๋ฉ”ํ„ฐ(parameter)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.
console.log๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด, ์‚ฌ์šฉํ• ๋•Œ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๋„ฃ์ฃ ,
console.log("helloworld") ์—ฌ๊ธฐ์—์„œ๋Š” helloworld๊ฐ€ ์ธ์ž์ž…๋‹ˆ๋‹ค.

์šฐ๋ฆฌ์˜ ํ•จ์ˆ˜๋„ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋ด…์‹œ๋‹ค.

function sampleFunc(arg1, arg2) {
    console.log(arg1, arg2);
}

sampleFunc(1, "hello!");

//์ถœ๋ ฅ ๊ฒฐ๊ณผ
//1 hello!

์—ฌ๊ธฐ์—์„œ arg1, arg2๊ฐ€ ์ธ์ž๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
์ธ์ž๋ž€, ๋‹ค์‹œ๋งํ•˜์ž๋ฉด, ์™ธ๋ถ€์—์„œ ์ฃผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์ž…๊ตฌ๊ฐ™์€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

์ž, ๊ทธ๋Ÿฐ๋ฐ ์ด์ œ ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ๋“ค์–ด๊ฐ€๋ ค๊ณ  ํ• ๋•Œ, undefined ์ƒํƒœ, ์ฆ‰ ์•„๋ฌด๋Ÿฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

function sampleFunc(arg1, arg2) {
    console.log(arg1, arg2);
}

const result = sampleFunc(1, "hello!");
console.log("result ๋ณ€์ˆ˜์˜ ๊ฐ’: ",result);

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// 1 hello!
// result ๋ณ€์ˆ˜์˜ ๊ฐ’:  undefined

์ด์œ ๋Š”, ํ•จ์ˆ˜์— return๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
console.log๋Š” ๊ฐ’์„ ์ฝ˜์†” ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ํ•จ์ˆ˜ ์•ˆ์—๋Š” console.log๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ๋˜์–ด์žˆ์œผ๋‹ˆ, ๋‹น์—ฐํžˆ ์ฝ˜์†”ํ™”๋ฉด์—์„œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ํ™•์ธ ๋˜์ง€๋งŒ, return์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ธด ๊ฐ’์€ undefined ์ฆ‰! ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š”๊ฒƒ์ด์ฃ . ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ return์„ ์ถ”๊ฐ€ํ•ด์ค์‹œ๋‹ค.

3. return๊ฐ’์ด ์žˆ๋Š” function ๋งŒ๋“ค๊ธฐ

์ด์ œ ํ•จ์ˆ˜์—์„œ ์ •์˜๋œ ์‹คํ–‰๋ฌธ์ด ๋ชจ๋‘ ๋๋‚˜๊ณ , ๊ฒฐ๊ณผ๊ฐ’์„ ๋Œ๋ ค์ค„ ์ˆ˜ ์žˆ๋„๋ก return์„ ๋„ฃ์Šต๋‹ˆ๋‹ค.
backtick(`)๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์ฒ˜์Œ๋ณด์‹œ๋Š”๋ถ„์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ๋ฅผ ํ™œ์„ฑํ™” ํ•ด์„œ ๋‚ด์šฉ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”!

๋”๋ณด๊ธฐ

๋ฐฑํ‹ฑ(backtick)/๋ฐฑ์ฟผํŠธ(backquote) ` ๋ž€?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ `๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„๋œ๊ฒƒ์€ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด(Template literals) ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์ค„๋ฐ”๊ฟˆ์„ ํŽธํ•˜๊ฒŒ ํ•˜๊ณ , ๋ฌธ์ž์—ด ๋‚ด๋ถ€์— ์ธ์ž๊ฐ’์„ ํŽธํ•˜๊ฒŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ์ถœ๋ ฅ๋˜๋Š” 1, 2, 3 ๋ชจ๋‘ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์ธ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ํŽธ์˜์„ฑ์„ ๋ณด๋ฉด ํ›จ์”ฌ ํŽธ๋ฆฌํ•˜์ฃ . ์ค‘๊ฐ„์ค‘๊ฐ„์— + , ์™€ ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ๋„ฃ์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋„์–ด์“ฐ๊ธฐ๋„ ๊ฐ€๋Šฅํ•ด์„œ ์•„์ฃผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!

function backtickSample(email,num) {
    console.log("1. ์ด๋ฉ”์ผ๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”. *์ด๋ฉ”์ผ:", email, "*์ „ํ™”๋ณ€ํ˜ธ:",num);
    console.log("2. ์ด๋ฉ”์ผ๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”. *์ด๋ฉ”์ผ: " + email + " *์ „ํ™”๋ณ€ํ˜ธ: " + num);
    console.log(`3. ์ด๋ฉ”์ผ๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”. *์ด๋ฉ”์ผ: ${email} *์ „ํ™”๋ณ€ํ˜ธ: ${num}`);
}

backtickSample("test@test.com","010-1123-1112");

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
// 1. ์ด๋ฉ”์ผ๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”. *์ด๋ฉ”์ผ: test@test.com *์ „ํ™”๋ณ€ํ˜ธ: 010-1123-1112
// 2. ์ด๋ฉ”์ผ๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”. *์ด๋ฉ”์ผ: test@test.com *์ „ํ™”๋ณ€ํ˜ธ: 010-1123-1112
// 3. ์ด๋ฉ”์ผ๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”. *์ด๋ฉ”์ผ: test@test.com *์ „ํ™”๋ณ€ํ˜ธ: 010-1123-1112
function sampleFunc(arg1, arg2) {
    return `${arg1}, ${arg2}`;
}

const result = sampleFunc(1, "hello!");
console.log("result ๋ณ€์ˆ˜์˜ ๊ฐ’: ",result);

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
// result ๋ณ€์ˆ˜์˜ ๊ฐ’:  1, hello!

์ž, ์ด์ œ result ๋ณ€์ˆ˜์—๋„ ๊ฐ’์ด ์ž˜ ๋“ค์–ด๊ฐ”๋„ค์š”!

 

4. ์‚ฌ์น™์—ฐ์‚ฐ function ๋งŒ๋“ค๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ

console.log์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค.

const calculator = {
    plus: function(a, b) {
        return a+b
    },
    minus: function(a, b) {
        return a-b
    },
    multiply: function(a, b) {
        return a*b
    },
    devide: function(a, b) {
        return a/b
    }
}

const plusResult = calculator.plus(1,2);
const minusResult = calculator.minus(20,2);
console.log(plusResult);
console.log(minusResult);

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
// 3
// 18

์ด์ œ console.log์ฒ˜๋Ÿผ calculator.plus๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!!!

728x90