thumbnail
μ—˜λ¦¬λ¨ΌνŠΈμ™€ μ»΄ν¬λ„ŒνŠΈ
Front-End
2023.07.20.

μ—˜λ¦¬λ¨ΌνŠΈ

Key

KeyλŠ” Reactμ—μ„œ λ™μ μœΌλ‘œ μƒμ„±λ˜λŠ” μ»΄ν¬λ„ŒνŠΈ λ¦¬μŠ€νŠΈμ—μ„œ 각 μš”μ†Œκ°€ κ³ μœ ν•˜κ²Œ μ‹λ³„λ˜λ„λ‘ λ•λŠ” 역할을 ν•©λ‹ˆλ‹€. KeyλŠ” Reactκ°€ μ»΄ν¬λ„ŒνŠΈ 리슀트λ₯Ό μ—…λ°μ΄νŠΈν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€. Keyκ°€ μ—†λŠ” 경우 리슀트의 μˆœμ„œκ°€ λ°”λ€Œκ±°λ‚˜ μ•„μ΄ν…œμ΄ μΆ”κ°€/μ‚­μ œλ  λ•Œ ReactλŠ” 이전에 λ Œλ”λ§λœ κ²°κ³Όλ₯Ό 기반으둜 변경사항을 κ³„μ‚°ν•˜κ²Œ 되고, μ΄λ•Œ 변경사항이 μ˜ˆμƒκ³Ό λ‹€λ₯Ό 수 μžˆμŠ΅λ‹ˆλ‹€. Keyλ₯Ό μ‚¬μš©ν•˜λ©΄ ReactλŠ” 각 μ•„μ΄ν…œμ΄ κ³ μœ ν•œ IDλ₯Ό 가지고 있기 λ•Œλ¬Έμ— 변경사항을 더 μ •ν™•ν•˜κ²Œ 계산할 수 μžˆμŠ΅λ‹ˆλ‹€.

var items = ['μ—°μ–΄ 500그램', '잣 1 μ»΅', '버터 상좔 2μ»΅', '옐둜 μŠ€μΏΌμ‹œ(Yellow Squash, ν˜Έλ°•μ˜ ν•œ μ’…λ₯˜) 1개', '올리브 였일 1/2 μ»΅', '마늘 3 μͺ½']
var dish = React.createElement('ul', {className: 'ingredients'},
    items.map(ingredients => React.createElement('li', null, ingredients))
);
console.log('dish', dish);

function init() {
    var container = document.querySelector('.react-container');

    ReactDOM.render(dish, container);
}

window.addEventListener('load', init);
var items = ['μ—°μ–΄ 500그램', '잣 1 μ»΅', '버터 상좔 2μ»΅',
    '옐둜 μŠ€μΏΌμ‹œ(Yellow Squash, ν˜Έλ°•μ˜ ν•œ μ’…λ₯˜) 1개',
    '올리브 였일 1/2 μ»΅', '마늘 3 μͺ½']
var dish = React.createElement('ul', {className: 'ingredients'},
    items.map((ingredients, i) => React.createElement('li', {key: i}, ingredients))
);
console.log('dish', dish);

function init() {
    var container = document.querySelector('.react-container');
    ReactDOM.render(dish, container);
}

window.addEventListener('load', init);

μœ„ μ½”λ“œλŠ” Reactλ₯Ό μ‚¬μš©ν•˜μ—¬ itemsλΌλŠ” 리슀트λ₯Ό ν‘œμ‹œν•˜λŠ” <ul> μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μƒμ„±ν•˜λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€. items 리슀트λ₯Ό map() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ <li> μ—˜λ¦¬λ¨ΌνŠΈλ‘œ λ³€ν™˜ν•˜κ³ , key prop을 μ‚¬μš©ν•˜μ—¬ 각각의 <li> μ—˜λ¦¬λ¨ΌνŠΈμ— κ³ μœ ν•œ ID 값을 λΆ€μ—¬ν•©λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•¨μœΌλ‘œμ¨ ReactλŠ” μ—…λ°μ΄νŠΈμ‹œ λ”μš± μ •ν™•ν•˜κ²Œ 바뀐 뢀뢄을 인식할 수 있게 λ©λ‹ˆλ‹€. λ§ˆμ§€λ§‰μœΌλ‘œ, ReactDOM.render() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ dish μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό .react-container에 λ Œλ”λ§ν•©λ‹ˆλ‹€.

μ»΄ν¬λ„ŒνŠΈ

μ»΄ν¬λ„ŒνŠΈλŠ” UIλ₯Ό κ΅¬μ„±ν•˜λŠ” 독립적인 λͺ¨λ“ˆμž…λ‹ˆλ‹€. μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ UIλ₯Ό μ—¬λŸ¬ 개의 μž¬μ‚¬μš© κ°€λŠ₯ν•œ 쑰각으둜 λ‚˜λˆŒ 수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό 톡해 μ½”λ“œμ˜ μž¬μ‚¬μš©μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ„±μ΄ μ¦κ°€ν•˜κ²Œ λ©λ‹ˆλ‹€.

Reactμ—μ„œ λͺ¨λ“  UI μš”μ†ŒλŠ” μ»΄ν¬λ„ŒνŠΈλ‘œ λ‚˜λˆŒ 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ λ‚˜λˆˆ μ»΄ν¬λ„ŒνŠΈλ“€μ€ μ€‘μ²©ν•˜μ—¬ μ‚¬μš©ν•  수 있으며, 각각의 μ»΄ν¬λ„ŒνŠΈλŠ” λ…λ¦½μ μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ

class Greeting extends React.Component {
    render() {
        return <h1>Hello, {this.props.name}!</h1>;
    }
}

ReactDOM.render(
    <Greeting name="React"/>,
    document.getElementById('root')
);

μœ„ μ½”λ“œλŠ” Greetingμ΄λΌλŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό μƒμ„±ν•˜κ³ , ReactDOM.render() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ Greeting μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•˜λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€. Greeting μ»΄ν¬λ„ŒνŠΈλŠ” name prop을 λ°›μ•„μ™€μ„œ Hello, {name}!μ΄λΌλŠ” λ©”μ‹œμ§€λ₯Ό ν‘œμ‹œν•©λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ name prop이 변경될 λ•Œλ§ˆλ‹€ μ»΄ν¬λ„ŒνŠΈκ°€ μ—…λ°μ΄νŠΈλ˜μ–΄ μƒˆλ‘œμš΄ λ©”μ‹œμ§€λ₯Ό ν‘œμ‹œν•˜κ²Œ λ©λ‹ˆλ‹€.

function Button(props) {
    return (
        <button onClick={props.onClick}>
            {props.label}
        </button>
    );
}

class Counter extends React.Component {
    constructor(props) {
        super(props);
        this.state = {count: 0};
    }

    handleClick() {
        this.setState(state => ({
            count: state.count + 1
        }));
    }

    render() {
        return (
            <div>
                <p>You clicked {this.state.count} times.</p>
                <Button
                    onClick={() => this.handleClick()}
                    label="Click me"
                />
            </div>
        );
    }
}

ReactDOM.render(
    <Counter/>,
    document.getElementById('root')
);

μœ„ μ½”λ“œλŠ” Buttonκ³Ό CounterλΌλŠ” 두 개의 μ»΄ν¬λ„ŒνŠΈλ₯Ό μƒμ„±ν•˜κ³ , ReactDOM.render() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ Counter μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•˜λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€. Button μ»΄ν¬λ„ŒνŠΈλŠ” onClick prop을 λ°›μ•„μ™€μ„œ λ²„νŠΌμ„ ν΄λ¦­ν–ˆμ„ λ•Œ onClick ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€. Counter μ»΄ν¬λ„ŒνŠΈλŠ” count stateλ₯Ό 가지고 있으며, λ²„νŠΌμ„ 클릭할 λ•Œλ§ˆλ‹€ count 값을 μ¦κ°€μ‹œν‚΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ ν™”λ©΄μ—λŠ” β€˜You clicked {count} timesβ€™λΌλŠ” λ©”μ‹œμ§€μ™€ β€˜Click meβ€™λΌλŠ” λ²„νŠΌμ΄ ν‘œμ‹œλ©λ‹ˆλ‹€.

ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈ

ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈλŠ” ν•¨μˆ˜λ‘œ κ΅¬ν˜„λœ μ»΄ν¬λ„ŒνŠΈμ΄λ©°, propsλ₯Ό 인자둜 λ°›μ•„μ„œ JSXλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

function Welcome(props) {
    return <h1>Hello, {props.name}</h1>;
}

ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈλŠ” μƒνƒœ(state)λ₯Ό 가지지 μ•ŠμœΌλ©°, 라이프사이클 λ©”μ„œλ“œ(componentDidMount, componentWillUnmount, λ“±)λ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ μ»΄ν¬λ„ŒνŠΈμ˜ 역할이 λ‹¨μˆœν•œ 경우, ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈ λŒ€μ‹  ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 더 νš¨μœ¨μ μž…λ‹ˆλ‹€.

ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ˜ μž₯점:

  • μ½”λ“œκ°€ κ°„κ²°ν•˜κ³  λͺ…ν™•ν•©λ‹ˆλ‹€.
  • ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλ³΄λ‹€ μ„±λŠ₯이 더 μ’‹μŠ΅λ‹ˆλ‹€.
  • μƒνƒœλ₯Ό 가지지 μ•ŠμœΌλ―€λ‘œ 예츑 κ°€λŠ₯ν•˜λ©° ν…ŒμŠ€νŠΈν•˜κΈ° μ‰½μŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ˜ 단점:

  • 라이프사이클 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
  • μƒνƒœλ₯Ό 가지지 μ•ŠμœΌλ―€λ‘œ, μƒνƒœλ₯Ό λ³€κ²½ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈ

ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλŠ” React.Component 클래슀λ₯Ό μƒμ†λ°›μ•„μ„œ κ΅¬ν˜„λ©λ‹ˆλ‹€. render() λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜μ—¬ JSXλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

class Welcome extends React.Component {
    render() {
        return <h1>Hello, {this.props.name}</h1>;
    }
}

s

ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλŠ” μƒνƒœ(state)λ₯Ό 가지며, 라이프사이클 λ©”μ„œλ“œ(componentDidMount, componentWillUnmount, λ“±)λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μƒνƒœλ₯Ό λ³€κ²½ν•  수 μžˆμœΌλ―€λ‘œ, μƒνƒœμ— 따라 UIλ₯Ό λ³€κ²½ν•΄μ•Ό ν•˜λŠ” 경우 ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈμ˜ μž₯점:

  • μƒνƒœ(state)λ₯Ό κ°€μ§€λ―€λ‘œ, μƒνƒœμ— 따라 UIλ₯Ό λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 라이프사이클 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈμ˜ 단점:

  • μ½”λ“œκ°€ λ³΅μž‘ν•˜κ³ , ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈλ³΄λ‹€ μ„±λŠ₯이 더 λ–¨μ–΄μ§‘λ‹ˆλ‹€.
  • μƒνƒœ(state)λ₯Ό κ°€μ§€λ―€λ‘œ μ˜ˆμΈ‘ν•˜κΈ° μ–΄λ ΅κ³ , ν…ŒμŠ€νŠΈν•˜κΈ° μ–΄λ ΅μŠ΅λ‹ˆλ‹€.

μ˜ˆμ‹œ

// ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈ 예제
function Welcome(props) {
    return <h1>Hello, {props.name}!</h1>;
}

// ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈ 예제
class Welcome extends React.Component {
    render() {
        return <h1>Hello, {this.props.name}!</h1>;
    }
}

μœ„ μ˜ˆμ œμ—μ„œ Welcome μ»΄ν¬λ„ŒνŠΈλŠ” ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ™€ ν΄λž˜μŠ€ν˜• μ»΄ν¬λ„ŒνŠΈ 두 가지 λ°©μ‹μœΌλ‘œ κ΅¬ν˜„λ˜μ—ˆμŠ΅λ‹ˆλ‹€. Welcome μ»΄ν¬λ„ŒνŠΈλŠ” propsλ₯Ό λ°›μ•„μ„œ Hello, {props.name}!μ΄λΌλŠ” λ©”μ‹œμ§€λ₯Ό ν‘œμ‹œν•©λ‹ˆλ‹€.

μ˜›λ‚  방식과 μš”μ¦˜ 클래슀

    // μ˜›λ‚ λ°©μ‹ -> μƒμ„±μž ν•¨μˆ˜λ₯Ό μ΄μš©ν•œ λͺ…μ‹œμ ...
    // ν•¨μˆ˜μ΄λ¦„μ΄ λŒ€λ¬Έμžλ‘œ μ‹œμž‘ν•˜λ©΄ μƒμ„±μž ν•¨μˆ˜
function Vacation(destination, length) {
    this.destination = destination;
    this.length = length;
}

Vacation.prototype.print = function () {
    console.log('이번 νœ΄κ°€λŠ” ' + this.destination + '으둜 ' + this.length + '떠남')
};

var obj = new Vacation('ν•˜μ™€μ΄', 10);
obj.print();
    class Vacation {
    constructor(destination, length) {
        this.destination = destination;
        this.length = length;
    }

    print() {
        console.log('이번 νœ΄κ°€λŠ” ' + this.destination + '으둜 ' + this.length + '떠남')
    };
}

obj = new Vacation('ν•˜μ™€μ΄', 10);
obj.print();

객체 ꡬ쑰 λΆ„ν•΄

객체 ꡬ쑰 λΆ„ν•΄λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λŠ₯ 쀑 ν•˜λ‚˜λ‘œ, κ°μ²΄μ—μ„œ 속성을 μΆ”μΆœν•˜κ³  λ³€μˆ˜μ— ν• λ‹Ήν•˜λŠ” 것을 더 κ°„κ²°ν•˜κ³  가독성 높은 λ°©λ²•μœΌλ‘œ ν•  수 있게 ν•©λ‹ˆλ‹€.

λ‹€μŒμ€ μ˜ˆμ‹œμž…λ‹ˆλ‹€:

const person = {
    name: 'John',
    age: 30,
    location: 'New York'
};

const {name, age, location} = person;

console.log(name);      // 좜λ ₯: 'John'
console.log(age);       // 좜λ ₯: 30
console.log(location);  // 좜λ ₯: 'New York'

이 μ˜ˆμ‹œμ—μ„œλŠ” personμ΄λΌλŠ” 객체가 μ„Έ 개의 속성, 즉 name, age, location을 가지고 μžˆμŠ΅λ‹ˆλ‹€. 객체 ꡬ쑰 λΆ„ν•΄λ₯Ό μ‚¬μš©ν•˜μ—¬ μ΄λŸ¬ν•œ 속성듀을 μΆ”μΆœν•˜κ³  같은 μ΄λ¦„μ˜ λ³€μˆ˜μ— ν• λ‹Ήν•©λ‹ˆλ‹€. 그러면 이 λ³€μˆ˜λ“€μ„ μ‚¬μš©ν•˜μ—¬ κ°μ²΄μ—μ„œ ν•΄λ‹Ή 값을 κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

객체 ꡬ쑰 λΆ„ν•΄λŠ” ν•¨μˆ˜ λ§€κ°œλ³€μˆ˜λ₯Ό λ‹€λ£° λ•Œ 맀우 μœ μš©ν•©λ‹ˆλ‹€. λ‹€μŒμ€ μ˜ˆμ‹œμž…λ‹ˆλ‹€:

function printPersonInfo({name, age, location}) {
    console.log(`Name: ${name}`);
    console.log(`Age: ${age}`);
    console.log(`Location: ${location}`);
}

const person = {
    name: 'John',
    age: 30,
    location: 'New York'
};

printPersonInfo(person);

이 μ˜ˆμ‹œμ—μ„œλŠ” 객체λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ°›λŠ” printPersonInfo ν•¨μˆ˜λ₯Ό μ •μ˜ν•˜κ³ , 객체 ꡬ쑰 λΆ„ν•΄λ₯Ό μ‚¬μš©ν•˜μ—¬ name, age, location 속성을 μΆ”μΆœν•©λ‹ˆλ‹€. 그런 λ‹€μŒ 이 λ³€μˆ˜λ“€μ„ μ‚¬μš©ν•˜μ—¬ κ°μ²΄μ—μ„œ ν•΄λ‹Ή 값을 좜λ ₯ν•©λ‹ˆλ‹€.

객체 ꡬ쑰 λΆ„ν•΄λŠ” μ€‘μ²©λœ 객체와 λ°°μ—΄μ—μ„œλ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‹€μŒμ€ μ˜ˆμ‹œμž…λ‹ˆλ‹€:

const person = {
    name: 'John',
    age: 30,
    location: {
        city: 'New York',
        state: 'NY'
    },
    hobbies: ['reading', 'traveling', 'hiking']
};

const {name, location: {city}, hobbies: [firstHobby]} = person;

console.log(name);        // 좜λ ₯: 'John'
console.log(city);        // 좜λ ₯: 'New York'
console.log(firstHobby);  // 좜λ ₯: 'reading'

이 μ˜ˆμ‹œμ—μ„œλŠ” μ€‘μ²©λœ 속성과 배열이 ν¬ν•¨λœ 객체λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 객체 ꡬ쑰 λΆ„ν•΄λ₯Ό μ‚¬μš©ν•˜μ—¬ μ€‘μ²©λœ location μ†μ„±μ—μ„œ city 속성과 hobbies λ°°μ—΄μ—μ„œ 첫 번째 μ·¨λ―Έλ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.

μ΄κ΄„μ μœΌλ‘œ, 객체 ꡬ쑰 λΆ„ν•΄λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κ°•λ ₯ν•œ κΈ°λŠ₯ 쀑 ν•˜λ‚˜λ‘œ, μ½”λ“œλ₯Ό 더 κ°„κ²°ν•˜κ³  가독성 μ’‹κ²Œ λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€.

λ¦¬ν„°λŸ΄κ°μ²΄ = μ•”μ‹œμ  λ°©λ²•μœΌλ‘œ λ§Œλ“  객체 β†’ λ¦¬ν„°λŸ΄κ°μ²΄ κ°œμ„ 

λ¦¬ν„°λŸ΄κ°μ²΄λŠ” λ³€μˆ˜μ— ν• λ‹Ήλ˜λŠ” 객체 ν‘œκΈ°λ²• 쀑 ν•˜λ‚˜λ‘œ, μ€‘κ΄„ν˜Έ({}) μ•ˆμ— 속성(key)κ³Ό κ°’(value)을 μ‰Όν‘œ(,)둜 κ΅¬λΆ„ν•˜μ—¬ μž‘μ„±ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이λ₯Ό 톡해 객체λ₯Ό 쉽고 κ°„νŽΈν•˜κ²Œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, λ‹€μŒκ³Ό 같은 객체가 μžˆλ‹€κ³  κ°€μ •ν•΄λ΄…μ‹œλ‹€.

const person = {
    name: 'John',
    age: 30,
    city: 'New York'
};

μœ„ μ˜ˆμ‹œμ—μ„œ person은 λ¦¬ν„°λŸ΄κ°μ²΄λ‘œ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 객체 λ¦¬ν„°λŸ΄ ν‘œκΈ°λ²•μ„ μ‚¬μš©ν•˜μ—¬ name, age, city 속성을 μ •μ˜ν•˜κ³ , ν•΄λ‹Ή κ°’μœΌλ‘œ 각각 'John', 30, 'New York' 을 ν• λ‹Ήν–ˆμŠ΅λ‹ˆλ‹€.

λ¦¬ν„°λŸ΄κ°μ²΄λŠ” 객체λ₯Ό μƒμ„±ν•˜κ³  μ΄ˆκΈ°ν™”ν•˜λŠ” 데 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 객체 λ¦¬ν„°λŸ΄ ν‘œκΈ°λ²•μ€ λ³΅μž‘ν•œ κ°μ²΄λ‚˜ μ€‘μ²©λœ 객체λ₯Ό λ§Œλ“€ λ•Œλ„ μœ μš©ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, λ‹€μŒκ³Ό 같은 μ€‘μ²©λœ 객체λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

const person = {
    name: 'John',
    age: 30,
    address: {
        street: '123 Main St',
        city: 'New York',
        state: 'NY'
    }
};

μœ„ μ˜ˆμ‹œμ—μ„œ person κ°μ²΄λŠ” address 속성을 가지고 있으며, 이 속성은 λ˜λ‹€λ₯Έ μ€‘μ²©λœ 객체λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ 객체λ₯Ό μ€‘μ²©ν•˜μ—¬ κ΅¬μ„±ν•˜λ©΄, λ³΅μž‘ν•œ 데이터λ₯Ό 보닀 μ‰½κ²Œ 관리할 수 μžˆμŠ΅λ‹ˆλ‹€.

λ¦¬ν„°λŸ΄κ°μ²΄λŠ” μ½”λ“œλ₯Ό 더 κ°„κ²°ν•˜κ³  가독성 μ’‹κ²Œ λ§Œλ“€μ–΄μ£ΌλŠ” μž₯점이 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ, 객체λ₯Ό 생성할 λ•Œ 일반적으둜 μ‚¬μš©λ˜λŠ” 방식이기 λ•Œλ¬Έμ— μ΅μˆ™ν•΄μ§ˆ ν•„μš”κ°€ μžˆμŠ΅λ‹ˆλ‹€.

객체 λ¦¬ν„°λŸ΄ κ°œμ„ 

var name = '손ν₯λ―Ό';
var gender = 'λ‚¨μž';
var age = 31;
var play = function () {
    console.log(this.name + '이 좕ꡬλ₯Ό λ•Œλ¦Ό')
}

var person = {name, gender, age, play};
person.play()

// λ³΄μ΄λŠ” κ°’ -> 손ν₯민이 좕ꡬλ₯Ό λ•Œλ¦Ό
Thank You for Visiting My Blog, Have a Good Day πŸ˜†
Β© 2023 Developer JaeHyeon, Powered By Gatsby.