const orginal = { a: 1, b: 2 };
const copy = { ...original, c: 3 };
const { a, ...noA } = copy; // noA => { b: 2, c: 3 } !
// 구조 분해 사용해서 a 없는 애 get
Object.assign 대신 Spread Operator 사용하기.
const someStack = [];
// bad
someStack[someStack.length] = 'ababab';
// good
someStack.push('ababab');
const itemsCopy = [...items];
const foo = document.querySelectorAll('.foo');
// good
const nodes = Array.from(foo);
// best
const nodes = [...foo];
const arrLike = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
const arr = Array.from(arrLike);
// bad
function getFullName(user) {
const firstName = user.firstName;
const lastName = user.lastName;
return `${firstName} ${lastName}`;
}
// good
function getFullName(user) {
const { firstName, lastName } = user;
return `${firstName} ${lastName}`;
}
// best
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
bad 코드 -> 중복되는 코드가 두 줄
good 코드 -> 중복을 하나로 줄임
best -> function의 parameter 단에서 비구조화를 해버림.
// bad
function processInput(input) {
// sth process...
return [left, right, top, bottom];
}
// 반환되는 데이터의 순서를 생각해야하는 귀찮음이 발생
// 난 함수의 리턴값으로 나온 배열 중 left와 top 만을 원하는데 해당 index 위치를 기억해야 하니깐..
const [left, _, top] =processInput(input);
// good
function processInput(input) {
// sth process...
retrun { left, right, top, bottom };
}
// 객체 비구조화 사용해서 left와 top 만 쉽게 꺼내 쓰기
// 변수 명만 알고있으면 된다는 장점.
const { left, top } = processInput(input);
// bad
[1,2,3].map(number => {
const nextNumber = number + 1;
`A string containing the ${nextNumber}.`;
});
// good
[1,2,3].map(number => `A string containing the ${number}.`);
// good
[1,2,3].map((number) => {
const nextNumber = number + 1;
return `A string containing the ${nextNumber}.`;
});
// good
// 객체의 배열로 만들고 싶을 때 괄호 안에 객체 암시적 리턴해줘야 함
[1,2,3].map((number, index) => ({
[index]: number;
}));
// bad
// 맨날 이렇게 썼었는데 앞으로는 고쳐야겠다.
[1,2,3].map(x => x * x);
// good
// 이렇게 인자에 소괄호 붙여주는 습관을 들이자.
[1,2,3].map((x) => x*x);
// bad
(foo) =>
bar;
(foo) =>
(bar);
// good
(foo) => bar;
(foo) => (bar);
(foo) => (
bar
)
const luke = {
jedi: true,
age: 28,
};
// bad
const isJedi = luke['jedi'];
// good
const isJedi = luke.jedi;
const luke = {
jedi: true,
age: 28,
};
function getProp(prop) {
return luke[prop];
}
const isJedi = getProp('jedi');
https://github.com/naver/eslint-config-naver/blob/master/STYLE_GUIDE.md#types
naver 가 쓴 code convention이 더 나은듯.