๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Frontend/JavaScript

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] Call by Value, Call by reference, Call by sharing

by YWTechIT 2021. 7. 8.
728x90

๐Ÿ“ Call by Value, Call by reference, Call by sharing

CS ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉฐ Call by Value, Call by Reference์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ณ  ์žˆ๋Š”๋ฐ, ํ—ท๊ฐˆ๋ฆฌ๋Š” ๊ธ€๋“ค์ด ๋งŽ์•˜๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ JS์—์„œ๋Š” Call by reference, Call by sharing ๋‘ ์šฉ์–ด๋ณด๋‹ค Call by Value ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•Œ๋งž๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด์ž.

 

// ์›์‹œ๊ฐ’ ๋ณ€๊ฒฝ์‹œ๋„
function foo(argument){
    argument = 10;
    console.log(argument)    // 10
}

// ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜
function foo(){
    let argument = 5;
    argument = 10;
    console.log(argument)    // 10
}

let argument = 5;
foo(argument);

console.log(argument);    // 5

 

์›์‹œ๊ฐ’(Primitive type: string boolean number null undefined)์€ ์ฃผ์†Œ ๋Œ€์‹  ๊ฐ’ ์ž์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•œ argument์™€ ์‹ค์ œ๋กœ ํ•จ์ˆ˜์—์„œ ๋ฐ›์€ argument์˜ ๊ฐ’์€ ๋‹ค๋ฅธ ๊ฐ’์ด ๋œ๋‹ค. ๋•Œ๋ฌธ์— ์ฒซ๋ฒˆ์งธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ argument console.log๋Š” 10์ด๋˜๊ณ , ํ•จ์ˆ˜ ๋ฐ–์—์„œ์˜ argument console.log๋Š” 5๊ฐ€ ๋œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด, ์›์‹œ๊ฐ’(Primitive type)์ด ์•„๋‹Œ ๊ฐ์ฒด(Object)์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

// ๊ฐ์ฒดํƒ€์ž…์˜ ์†์„ฑ ๋ณ€๊ฒฝ
function foo(argument){
    argument.a = 5
    console.log(argument)    // { a : 5 }
}

// ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜
function foo(){
    let arguments = argument
    argument.a = 10
    console.log(arguments)    // { a : 5 }
}

let argument = {a: 5} 
foo(argument)
console.log(argument)    // { a : 5 }

 

๊ฐ์ฒดํƒ€์ž…์˜ ์ธ์ž๋Š” ์ฃผ์†Œ๊ฐ’์„ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๊ฐ€ ๋˜๋Š”๋ฐ, ์ด๋•Œ๋Š” ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณด๊ณ ์žˆ๋‹ค ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜๋‚ด๋ถ€์˜ argument๊ฐ€ ๋ฐ”๋€Œ๋ฉด ํ•จ์ˆ˜ ์™ธ๋ถ€์˜ argument๋„ ๊ฐ™์ด ๋ฐ”๋€๋‹ค. ์ฆ‰ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๊ฐ’๊ณผ ์™ธ๋ถ€์˜ ๊ฐ’์ด ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๊ฐ’์œผ๋กœ ๋ฐ”๋€๋‹ค. ๊ทธ๋Ÿผ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ค์Œ์€ ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ๊นŒ?

 

function foo(argument){
    argument = 5;
    console.log(argument)    // ?
}

let argument = {a: 10}
foo(argument)
console.log(argument)    // ?

 

์ฝ”๋“œ์˜ ํ๋ฆ„์„ ์‚ดํŽด๋ณด๋ฉด ์ด๋•Œ๋Š” ํ•จ์ˆ˜๋กœ ๋„˜์–ด๊ฐ€๋Š” argument๋Š” ๊ฐ์ฒด, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ argument๋Š” ์›์‹œ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค. ์ด๋•Œ๋Š” ์†์„ฑ์„ ์ˆ˜์ •ํ•œ๊ฒƒ์ด ์•„๋‹Œ ๊ฐ’ ์ „์ฒด๋ฅผ ๋ฐ”๊พธ๋Š”๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋‚ด๋ถ€๋Š” 5, ํ•จ์ˆ˜์™ธ๋ถ€๋Š” {a : 10}๋กœ ๋‚˜์˜ค๊ฒŒ๋œ๋‹ค.

 

๋งŒ์•ฝ, c, c++์™€ ๊ฐ™์ด call by reference๊ฐ€ ์กด์žฌํ•˜๋Š” ์–ธ์–ด๋ผ๋ฉด ๊ฐ’์ด ํ•จ์ˆ˜ ์™ธ๋ถ€์˜ ๊ฐ’๋„ 5๋กœ ๋ฐ”๋€Œ์—ˆ์„ ๊ฒƒ์ด๋‹ค. (๋Œ€์‹  call by sharing์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ ์ด๋Š” ์ •์‹ ๋ช…์นญ์ด ์•„๋‹ˆ๋ผ๋Š” ๋ง๋„ ์žˆ์–ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค.)

 

์ง€๊ธˆ๊นŒ์ง€์˜ ๋‚ด์šฉ์„ ํ† ๋Œ€๋กœ ์ •๋ฆฌํ•˜๋ฉด ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ˆ˜์ • ํ• ๋•Œ๋Š” ์ฐธ์กฐ ๊ด€๊ณ„์ง€๋งŒ ๊ฐ’ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ ๊ทธ ๊ด€๊ณ„๊ฐ€ ๊นจ์ง„๋‹ค๋ผ๊ณ  ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๋˜, ํ•จ์ˆ˜ ๋‚ด์—์„œ ์›์‹œ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ’์€ ๋ฐ”๋€Œ์ง€ ์•Š์ง€๋งŒ, ํ•จ์ˆ˜ ๋‚ด์—์„œ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ’์ด ๋ฐ”๋€๋‹ค.


๐Ÿ“‹ reference

  1. ZeroCho์˜ JS ์ค‘๊ธ‰ ๊ฐ•์ขŒ 12-1. call by value, call by reference, call by sharing
  2. MDN - ์›์‹œ ๊ฐ’
  3. MDN - Working_with_Objects
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€