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?