Object Literal Upgrades

const first = 'snickers';
const last = 'bos';
const age = 2;
const breed = 'King Charles Cav';
const dog = {
    first: first,
    last: last,
    age: age,
    breed: breed,
}

console.log(dog);

key ์™€ value์— ์„ค์ •๋œ name์ด ๊ฐ™๋‹ค๋ฉด!

const first = 'snickers';
const last = 'bos';
const age = 2;
const breed = 'King Charles Cav';
const dog = {
    first,
    last,
    age,
    breed,
}

console.log(dog);

ํ•˜๋‚˜์˜ ์ด๋ฆ„๋งŒ ๋‚จ๊ฒจ๋†“์•„๋„ ๋œ๋‹ค.

๋ฉ”์†Œ๋“œ์—์„œ

const modal = {
    create: function() {

    },
    open: function() {

    },
    close: function() {

    },
}

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

const modal = {
    create(selector) {

    },
    open(content) {

    },
    close(goodbye) {

    },
}

arrow function์„ ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค. 'o'

example 1

const key = 'pocketColor';
const value = '#ffc600';

const tShirt = {
    [key]: value
};

console.log(tShirt); // {pocketColor: "#ffc600"}

ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด ๋ณธ๋‹ค.

function invertColor(color) {
      return '#' + ("000000" + (0xFFFFFF ^ parseInt(color.substring(1),16)).toString(16)).slice(-6);
}

const key = 'pocketColor';
const value = '#ffc600';

const tShirt = {
    [key]: value,
    [`${key}Opposite`]: invertColor(value);
};

console.log(tShirt); // {pocketColor: "#ffc600"}

example 2

const keys = ['size', 'color', 'weight'];
const values = ['medium', 'red', 100];

const shirt = {
    [key.shift()]: key.shift(),
    [key.shift()]: key.shift(),
    [key.shift()]: key.shift(),
}

console.log(shirt);

Last updated

Was this helpful?