-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from learning-with/week14/jiho
지호 14주차
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# 26장 ES6 함수의 추가 기능 | ||
|
||
## 메서드 | ||
- ES6 메서드는 인스턴스를 생성할 수 없음(non-constructor) | ||
- ES6 이전의 방식은 사용하지 않는 것이 좋음(프로퍼티 값으로 익명 함수 표현식 할당 등) | ||
|
||
## 화살표 함수 | ||
- 화살표 함수는 콜백 함수로서 정의할 때 유용 | ||
```javascript | ||
[1, 2, 3].map(v => v * 2); | ||
``` | ||
- 화살표 함수는 함수 자체의 this 바인딩을 가지지 않음 | ||
따라서, 함수 내부에서 this 참조 시 상위 스코프의 this 참조(렉시컬 this) | ||
|
||
|
||
## Rest 파라미터 | ||
- 매개변수에 할당된 인수를 제외한 나머지 인수들로 구성된 배열 | ||
- Rest는 반드시 마지막 파라미터여야하며 하나만 선언 가능 | ||
- 함수 객체의 length 프로퍼티에 영향을 주지 않음 | ||
```javascript | ||
function foo(x, y, ...rest) {} | ||
console.log(foo.length); // 2 | ||
``` | ||
|
||
|
||
# 27장 배열 | ||
- 밀집배열: 동일한 크기의 메모리 공간이 빈틈없이 연속적으로 나열된 자료구조(하나의 데이터 타입으로 통일 및 연속적으로 인접) | ||
- 매우 효율적이며 빠른 동작 | ||
- 인덱스로 빠르게 접근 가능 | ||
- 요소 삽입/삭제할 때 연속성 유지를 위해 기존 요소 이동 필요 | ||
- 희소 배열: 메모리 공간이 동일한 크기를 가지지 않아도 되며, 연속적으로 이어져 있지 않은 자료구조 (자바스크립트의 배열은 희소 배열) | ||
- 해시 테이블로 구현된 객체로 인덱스로 접근 시 밀집 배열보다 느림 | ||
- 요소 삽입/삭제 시 밀집 배열보다 빠른 성능 | ||
|
||
- 배열은 같은 타입의 요소를 연속적으로 위치시키는 것이 최선(권장) | ||
|
||
## sort 함수 | ||
- 배열을 정렬하는 가장 흔한 방법 | ||
```javascript | ||
const nums = [3, 1, 2]; | ||
const sortedNums = nums.sort(); | ||
|
||
nums === sortedNums // true | ||
``` | ||
|
||
- 흔한 실수 | ||
```javascript | ||
// 정렬전 배열 내의 값을 내부적으로 문자로 변환하기 때문 | ||
[100, 3, 1, 20].sort(); // [1, 100, 20, 3] | ||
|
||
// 숫자 요소 정렬은 정렬 순서를 정의하는 비교함수를 인수로 전달 필요 | ||
[-3, 2, 0, 1, 3, -2, -1].sort((a, b) => a - b); // [-3, -2, -1, 0, 1, 2, 3] 오름차순 정렬 | ||
|
||
// 객체 배열 정렬 | ||
const countries = [ | ||
{ no: 1, code: "KR", name: "Korea" }, | ||
{ no: 2, code: "CA", name: "Canada" }, | ||
{ no: 3, code: "US", name: "United States" }, | ||
{ no: 4, code: "GB", name: "United Kingdom" }, | ||
{ no: 5, code: "CN", name: "China" }, | ||
]; | ||
|
||
// 객체를 문자열로 변환하면 [object Object]가 되어 모든 객체의 크기가 동일하다고 판단 | ||
countries.sort(); | ||
/** | ||
[ | ||
{ no: 1, code: "KR", name: "Korea" }, | ||
{ no: 2, code: "CA", name: "Canada" }, | ||
{ no: 3, code: "US", name: "United States" }, | ||
{ no: 4, code: "GB", name: "United Kingdom" }, | ||
{ no: 5, code: "CN", name: "China" }, | ||
] | ||
*/ | ||
|
||
// 객체간의 대소비교는 기준을 잘 정의해야함 | ||
countries.sort((a, b) => a.code.localeCompare(b.code)); | ||
/** | ||
[ | ||
{ no: 2, code: 'CA', name: 'Canada' }, | ||
{ no: 5, code: 'CN', name: 'China' }, | ||
{ no: 4, code: 'GB', name: 'United Kingdom' }, | ||
{ no: 1, code: 'KR', name: 'Korea' }, | ||
{ no: 3, code: 'US', name: 'United States' } | ||
] | ||
*/ | ||
``` |