ES6 Summary 2 - 클로저

ES6 속성 정리

본 정리본은 인프런의
모던 자바스크립트(javascript) 개발을 위한 ES6 강좌
를 바탕으로 작성되었습니다.

let, const

const라고 해서 값 변경이 불가능하다는 것은 아니다.

1
2
3
4
5
6
7
const array = [1, 2, 3, 4, 5];
array.push(6); //가능할까?

console.log(array);
/*
[ 1, 2, 3, 4, 5, 6 ]
*/

es6 string에 새로운 메서드들

  • 특정 문자열의 포함 여부를 알려주는 메서드들
1
2
3
4
let str = "hello world ^^";
console.log(str.startsWith("hello")); // true
console.log(str.endsWith("hello")); // false
console.log(str.includes("world")); // true

for in 과 for of

  • for in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const data = [1, 2, undefined, NaN, null, ""];
Array.prototype.getIndex = function() {};

for (let idx in data) {
console.log(data[idx]);
}

/**
1
2
undefined
NaN
null

[Function]
**/

for in의 문제점
자기가 가지고 있는 객체 이외의 상위의 프로토타입 객체가 나오게 되는 의도치 못한 결과가 발생할 수 있다.
그래서 array 문에서는 for in을 안 쓰는 것을 권장

  • for of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const data = [1, 2, undefined, NaN, null, ""];
Array.prototype.getIndex = function() {};

for (let value of data) {
console.log(value);
}
/**

1
2
undefined
NaN
null
**/

for of 를 배열이 아닌 문자 단위로도 순회 가능하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const str = "hello world";
for (let value of str) {
console.log(value);
}

/*
h
e
l
l
o

w
o
r
l
d
*/

spread operator(…)

spread operator, 펼침 연산자

1
2
3
4
let newData = [...pre];

console.log(newData); // [ 'apple', 'orange', 100 ]
console.log(pre === newData); // false
  • spread operator의 활용

    배열 합치기가 쉽다.

1
2
3
4
let pre = [100, 200, "hello", undefined];
let newData = [0, 1, 2, 3, ...pre, 4];

console.log(newData); // [ 0, 1, 2, 3, 100, 200, 'hello', undefined, 4 ]
  • spread operator 파라미터로 사용하기
1
2
3
4
5
6
function sum(a, b, c) {
return console.log(a + b + c);
}

let array = [1, 2, 3];
sum(...array); // 6

from 메서드

Comentarios

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×