๋ถˆ๋ณ€๊ฐ์ฒด(Immutable Object )

๐Ÿ“„ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋ž€?

๋ณ€์ˆ˜์— ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๋”๋ผ๋„, ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด

๋ถˆ๋ณ€์„ฑ ์—ฌ๋ถ€๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์˜ ๋Œ€์ƒ์€ ๋ฐ์ดํ„ฐ ์˜์—ญ๋ฉ”๋ชจ๋ฆฌ์ž…๋‹ˆ๋‹ค.

๋ถˆ๋ณ€ ๊ฐ์ฒด๋Š” ์–ธ์ œ ํ•„์š”ํ• ๊นŒ?

๋ชจ๋“  ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€๋ณ€์„ฑ์„ ๋ ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.

์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€๋ณ€์„ฑ์„ ๊ฐ€์งˆ ๋•Œ๋Š” ๋ฐ์ดํ„ฐ ์ž์ฒด๊ฐ€ ์•„๋‹Œ ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ๊ฐ’์œผ๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ์ฒด์— ๋ณ€๊ฒฝ์„ ๊ฐ€ํ•˜๋”๋ผ๋„ ์›๋ณธ ๊ฐ์ฒด๋Š” ๋ณ€ํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ๋ถˆ๋ณ€๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

var user = {
  name: "Jaenam",
  gender: "male",
};

var chanegeName = function (user, newName) {
  var newUser = user;
  newUser.name = newName;
  return newUser;
};

var user2 = chanegeName(user, "Jung");

if (user !== user2) {
  console.log("์œ ์ € ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); // ์‹คํ–‰๋˜์ง€ ์•Š์Œ
}
console.log(user.name, user2.name);
console.log(user === user2);

๊ฒฐ๊ณผ

Jung Jung
true

user๊ณผ newUser์ด ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์–ด newUser์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋ ๋•Œ ๋งˆ๋‹ค user์˜ ๋ฐ์ดํ„ฐ๋„ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ user๋ฅผ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ์ค„ ํ•„์š”์„ฑ์ด ๋ณด์ž…๋‹ˆ๋‹ค.

๐Ÿ“„ ์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ

1. ์–•์€ ๋ณต์‚ฌ

์ค‘์ฒฉ๋œ ๊ฐ์ฒด์—์„œ ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณต์‚ฌํ•  ๋•Œ ๊ทธ ์ฃผ์†Ÿ๊ฐ’๋งŒ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์–•์€ ๋ณต์‚ฌ๋ฅผ ํ•˜๋ฉด ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ์›๋ณธ๊ณผ ์‚ฌ๋ณธ์ด ๋ชจ๋‘ ๋™์ผํ•œ ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ , ์‚ฌ๋ณธ์„ ๋ฐ”๊พธ๋ฉด ์›๋ณธ์ด ๋ฐ”๋€Œ๊ณ  ์›๋ณธ์„ ๋ฐ”๊พธ๋ฉด ์‚ฌ๋ณธ์ด ๋ฐ”๋€Œ๋Š” ํ˜„์ƒ์ด ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค.

var copyObject = function (target) {
  var result = {};
  for (var prop in target) {
    result[prop] = target[prop];
  }
  return result;
};

var user = {
  name: "Jaenam",
  urls: {
    portfolio: "http://github.com/abc",
    blog: "http://blog.com",
    facebood: "http://facebook.com/abc",
  },
};
var user2 = copyObject(user);

user.name = "Jung";
console.log(user.name === user2.name); //false

user.urls.portfolio = "http://portfolio.com";
console.log(user.urls.portfolio === user2.urls.portfolio); // true

user2.urls.blog = "";
console.log(user.urls.blog === user2.urls.blog); //true

๊ฒฐ๊ณผ

false
true
true

user๊ฐ์ฒด์— ์ง์ ‘ ์†ํ•œ ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋ณต์‚ฌํ•ด์„œ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŒ๋“ค์–ด์ง„ ๋ฐ˜๋ฉด, ํ•œ ๋‹จ๊ณ„ ๋” ๋“ค์–ด๊ฐ„ urls์˜ ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋“ค์€ ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค..

user.urls์˜ ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค ํ•„์š”์„ฑ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

๊นŠ์€ ๋ณต์‚ฌ

๊นŠ์€ ๋ณต์‚ฌ๋Š” ์–•์€ ๋ณต์‚ฌ์—์„œ ํ•œ ๋‹จ๊ณ„ ๋” ๋ณต์‚ฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์ค‘์—์„œ ๊ทธ ๊ฐ’์ด ๊ธฐ๋ณธํ˜• ๋ฐ์ดํ„ฐ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•˜๊ณ , ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ์—๋Š” ๋‹ค์‹œ ๊ทธ ๋‚ด๋ถ€์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

var copyObjectDeep = function (target) {
  var result = {};
  if (typeof target === "object" && target !== null) {
    for (var prop in target) {
      result[prop] = copyObjectDeep(target[prop]);
    }
  } else {
    result = target;
  }
  return result;
};

target์ด ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆœํšŒํ•˜์—ฌ copyObjectDeepํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ , ์›๋ณธ๊ณผ ์‚ฌ๋ณธ์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ถœ์ฒ˜

  • ์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

Leave a comment