Object vs Record in ImmutableJS - seowonintech/react-native-todo-list GitHub Wiki

Object vs. Record in ImmutableJS

์™œ ์šฐ๋ฆฌ๋Š” Object ๋Œ€์‹  Record๋ฅผ ์จ์•ผํ•˜๋Š”๊ฐ€?

์•„๋ž˜ ์ฐธ๊ณ ์ž๋ฃŒ๋ฅผ ๋ณด๋ฉด์„œ Record๋Š” C์—์„œ ์“ฐ๋Š” Structure์™€ ๋น„์Šทํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ๋‚˜ ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

์ผ๋ฐ˜ Object ๋Œ€๋น„ Record์˜ ์žฅ์ 

  • They're self-documenting. (Record๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฉค๋ฒ„๋“ค์€ ํ•œ๋ฒˆ ์˜๋ฏธํŒŒ์•…์ด ์‰ฝ๊ฒŒ naming์„(self-documenting) ํ•˜๋ฉด ๊ณ„์† ๊ทธ๋ ‡๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ๋ฐ–์— ์—†์Œ)
  • They enforce strict code quality. (์ฒ˜์Œ์— ์ƒ์„ฑํ•  ๋•Œ์˜ ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†๋‹ค.)
  • They combine the benefits of ImmutableJS' Map() with normal JS objects. (์ผ๋ฐ˜ Object์— Map()์— ๋“ค์–ด์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋‚ด์žฅ ํ•จ์ˆ˜๋“ค๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค)
  • You can use getIn within maps and records. If a map contains records you can read record's values using getIn. (getIn์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Record์•ˆ์— ํฌํ•จ๋œ ๋‹ค๋ฅธ ImmutableJS ๊ฐ์ฒด์˜ ๊ฐ’์„ ์ง์ ‘ ์—‘์„ธ์Šค๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.)
'use strict';

import { Record, Map } from 'immutable';

export const Post = new Record({
    id: undefined,
    title: '',
    content: '',
    author: undefined
    comments: new Map()
});
const data = new Map({
  somePost: new Post({ title: 'some post' })
});

console.log(data.getIn(['somePost', 'title'])); // === 'some post';

์ฐธ๊ณ ์ž๋ฃŒ

โš ๏ธ **GitHub.com Fallback** โš ๏ธ