ES6 이후로 프론트엔드는 다양한 변화를 겪어왔다고 한다. 하지만 도대체 어떤 변화가 있었기에 그런말이 나오는지에 대해 한번 알아보자 !
includes()
메서드는 배열이 특정 요소를 포함하고 있는지 판별한다. boolean
const array1 = [1, 2, 3];
console.log(array1.includes(2)); // expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'); // true
console.log(pets.includes('at'); // false
console.log(Math.pow(7, 3)); // 7 * 7 * 7 = 343
// 7**3 = 343
console.log(Math.pow(2, 3)); // 2 * 2 * 2 = 8
// 2**3 = 8
async/await
등 많은 기능이 추가 되었다. 그래서 이를 제외하고 나머지 어떤 기능이 나왔는지에 대해 알아보자 String.prototye.padStart(), String.prototype.padEnd()
padStart()
메서드는 현재 문자열의 시작을 다른 문자열로 채워, 주어진 길이를 만족하는 새로운 문자열을 반환한다. 채워 넣기는 대상 문자열의 시작(좌측) 부터 적용 된다.string
const str1 = '5';
console.log(str1.padStart(2, '0') // "05"
const fullNumber ='203499002125581' // length : 16
const last4Digits = fullNumber.slice(-4); // 5581
const padStartNumber = last4Digits.padStart(fullNumber.length, '*'); // fullNumber.length : 16
const padEndNumber = last4Digits.padEnd(fullNumber.length, '*');
console.log(padStartNumber); // "************5581" 나머지 부분 왼쪽부터 '*'로 채운다.
console.log(padEndNumber); // "5581************" padEnd() 메서드는 오른쪽 부터 채워진다.
object.entries()
메서드는 for...in
와 같은 순서로 주어진 객체 자체의 enumerable 속성[key, value]
쌍의 배열을 반환 한다. for...in
루프가 다른점은 프로토 타입의 체인의 속성도 열거한다는 점이다.Array
const object = {
a: 'somestring'
b: 42
};
console.log(Object.entries(object)); // [['a', 'somestring'],['b', 42]]
for(const [key, value] of Object.entries(object)) {
console.log(`${key} : ${value}`);
// "a : somestring "
// "b : 42 "
Object.getOwnProperyDescriptors()
메서드는 주어진 객체 자신의 속성(즉, 객체에 직접 제공하는 속성, 객체의 프로토 타입 체인을 따라 존재하은 덕택에 제공하는게 아닌? )에 대한 속성 설명자(description)을 반환한다.속성의 설명자
없으면 undefined
let o, d;
o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, 'foo');
// d는 { configurable: true, enumerable: true, get: /*getter 함수 */, set: undefined }
o = { bar: 42 }
d = Object.getOwnPropertyDescriptor(o, 'bar');
// d는 { configurable: true, enumerable: true, value: 42, writable: true }
JavaScript
코드에 추가trailing comma
가 있을 시, 그 줄을 수정 없이 그대로 복사해서 쓸 수 있다JSON
에서는 허용을 하지 않는다.// 배열
let arr = [1, 2, 3, , ,];
arr.length; // 5
// forEact(), map()을 이용해 배열 순회시에는 빈 슬롯을 무시
arr.forEach((e) => console.log(e)) // 1, 2, 3
// 객체
let object = {
foo: "bar",
baz: "qwerty",
age: 42,
};
// 함수
function f(p) { }
function f(p, ) { }
(p) => { };
(p, ) => { };
Atomics
객체는 atomic 작업을 정적 메서드로 제공한다. SharedArrayBuffer
와 ArrayBuffer
객체와 함께 사용Atomic
연산은 예측 가능한 값을 쓰고 읽고, 다음 연산이 시작되기 전에 연산을 완료하여 중단 되지 않도록 한다.ArrayBuffer
객체와 유사하지만, 공유된 메모리상의 뷰를 생성하는데 사용될 수 있다.ArrayBuffer
와 달리, SharedArrayBuffer
는 분리 될 수 없다.SharedArrayBuffer
객체이다. 컨텐츠는 '0'으로 초기화 된다.ES6(2009년)
에서 배열
의 spread, rest가 가능해졌고, ES2018
에서는 객체에도 사용하는 것이 가능해졌다. const Object = {
a: 1,
b: 3,
c: 'cc',
d: 100
};
const {a, b, ...z} = Object;
console.log(a); // 1
console.log(b); // 3
console.log(z); // { c: 'cc', d: 100 }
const spread = {
...Object,
a: 10,
e: 30,
};
console.log(spread); // { a: 10, b: 3, c: 'cc', d: 100, e: 30}
(async() => {
const promises = [1, 2, 3].map((timer) => (
new Promise((resolve) => {
setTimeout(() => resolve(timer), timer*1000);
})
));
for await (const result of promises) {
console.log(result); // 1 2 3
}
})();
Promise 객체
let isLoading = true;
fetch(myRequest).then((response) => {
var contentType = response.headers.get("content-type");
if(contentType && contentType.includes("application/json")) {
return response.json()
}
throw new TypeError("Ooops!!!!, we haven't got JSON");
})
.then((json) => {/* process ypur JSON further */})
.catch((error) => { console.log(error} )
.finally(() => { isLoading = false });
const test = /foo.bar/s.test("foo\nbar"); // true
console.log(test); // true
let regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
let result = regex.exec("2021-06-21");
console.log(result); // ['2021-06-21', '2021', '06, '21', { day: ... }]
console.log(result.groups); // {day : '21', month: '06', year: '2021'}
룩비하인드 어서션
const regex = /(?<=\$)\d+(\.\d*)?/;
const result = regex.exec('$10.53');
[
"10.53",
".53"
]
const result2 = regex.exec('&10.53');
// null
유니코드 속성 이스케이프 \p{...}(긍정), \P{...}(부정) 형식으로 유니코드 속성 이스케이프를 사용할 수 있다.
const sentence = 'A ticket to 大阪 costs ¥2000 👌.';
const regexpEmojiPresentation = /\p{Emoji_Presentation}/gu;
console.log(sentence.match(regexpEmojiPresentation));
// Expected output: Array ["👌"]
const regexpNonLatin = /\P{Script_Extensions=Latin}+/gu;
console.log(sentence.match(regexpNonLatin));
// Expected output: Array [" ", " ", " 大阪 ", " ¥2000 👌."]
const regexpCurrencyOrPunctuation = /\p{Sc}|\p{P}/gu;
console.log(sentence.match(regexpCurrencyOrPunctuation));
// Expected output: Array ["¥", "."]
float()
메서드는 모든 하위 배열 요소를 지정한 깊이까지 재귀적으로 이어붙인 새로운 배열을 생성한다새로운 배열
const newArr = arr.flat([depth])
const arr1 = [1, 2, [3, 4]];
arr1.flat(); // [1, 2, 3, 4]
const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat(); // [1, 2, 3, 4, [5, 6]]
const arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2); // [1, 2, 3, 4, 5, 6]
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity); // [ 1, 2, 3, 4, 5, 6, ... ]
flatMap()
메서드는 먼저 매핑 함수를 사용해 각 엘리먼트에 대해 map 수행 후, 결과를 새로운 배열로 평탄화 하는 작업을 한다. flatMap
으 아주 유용하며 둘을 하나의 메소드로 병합할 때 조금 더 효율적이다.callback
함수의 결과이고, 깊이 1로 평탄화된 새로운 배열let arr1 = [1, 2, 3, 4];
arr1.map(x => [x * 2]); // [[2], [4], [6], [8]]
arr1.flatMap(x => [x * 2] // [2, 4, 6, 8]
// 한 레벨만 평탄화
arr1.flatMap(x => [[x * 2]]); // [[2], [4], [6], [8]]
let arr2 = ["it's Sunny in", "", "California"]
arr2.map(x => x.split("")); // [["it's", "Sunny", "in"], [""], ["Califonia"]]
arr2.flatMap(x => x.split("")); // ["it's", "Sunny", "in", "Califonia"]
Object.fromEntries()
메서드는 키값 쌍의 목록을 객체
로 바꾼다.const entries = new Map([['foo', 'bar'], [baz', 42]]);
const obj = Object.fromEntries(entries);
console.log(obj) // { foo: 'bar', baz: 42 }
trimStart()
메서드는 문자열 시작 부분의 공백을 제거한다. trimLeft()
라는 별칭을 가지고 있다.const greeting = ' Hello World! ';
console.log(greeting); //' Hello World! '
console.log(greeting.trimStart()); // 'Hello World! '
trimEnd()
메서드는 trimStart()
메서드와 반대로 오른쪽 공백을 제거한다.trimRight()
라는 별칭을 가지고 있다.const greeting = ' Hello World! ';
console.log(greeting); //' Hello World! '
console.log(greeting.trimEnd()); // ' Hello World!'
try {
...
} catch {
...
}
toString()
은 문자열을 반환하는 Object의 대표적인 방법이다.function Dog(name) {
this.name = name
}
const dog1 = new Dog("Barrey");
Dog.prototype.toString = function dogToString() {
return `{this.name}`
};
console.log(dog1.toString()); // Barrey
console.log(Symbol('desc').toString()); // "Symbol(desc)"
console.log(Symbol('desc').description); // "desc"
console.log(Symbol.iterator.description); // "Symbol.iterator"
console.log(Symbol.for('foo').description); // "foo"
console.log(`${Symbol('foo').description}bar`); // "foobar"
Atomics/ SharedArrayBuffer
멀티 스레드 환경에서 사용하는 메서드 인데 이와 관련해서 사용한적이 없어 개념 정리를 하면서 이해가 잘 안갔던 부분이다. 그리고 MDN 문서
의 예시를 같이 보면서하니 글로 읽는것보다 더 이해를 잘 할 수 있었다. 2016 ~ 2019
를 정리하였고 2부에서는 2020 ~ 2021
를 정리할 예정이다. 2부에서도 1부 못지않게 많은 유용한 메서드들이 나왔다. 그리고 우리가 지금 코딩을 하면서 사용하는 메서드들도 눈에 많이 띈다. 정확하지 않은 정보가 있을 수 있습니다. 댓글을 통해 피드백 주시면 적극적으로 반영하겠습니다🥲