메서드를 설명하기에 앞서, 자주 사용하는 배열 조작 메서드에 대해 짚고 넘어갈 점이 있습니
다. 애석하게도 배열 메서드 중 일부는 배열 ‘자체를’ 수정하며, 다른 일부는 새 배열을 반환합
니다. 예를 들어
push
는 배열 자체를 수정하며,
concat
은 새 배열을 반환합니다. 메서드 이름
에 이런 차이점에 대한 힌트가 전혀 없으므로 프로그래머가 전부 기억해야 합니다.
루비 같은 언어는 이름만 봐도 대상 자체를 수정하는지, 아니면 새로운 것을 반환하는지 알 수 있도
록 만들어져 있습니다. 예를 들어 루비에서 문자열
str
에서
str
.
downcase
를 호출하면 소문자 버전이 반
환되지만
str
자체는 바뀌지 않습니다. 반면
str
.
downcase
!를 호출하면
str
자체가 바뀝니다. 필자는 자
바스크립트 표준 라이브러리에서 어떤 메서드가 데이터 자체를 수정하고 어떤 메서드가 사본을 반환하는지
에 관한 아무런 단서도 제공하지 않는다는 점은 불필요한 기억을 강요하는 단점이라고 생각합니다.
8.2.1
배열의 처음이나 끝에서 요소 하나를 추가하거나 제거하기
배열의
처음
은 첫 번째 요소, 즉 인덱스가
0
인 요소를 말합니다. 마찬가지로 배열의
끝
은 인덱
스가 가장 큰 요소, 즉 배열이
arr
이라면
arr
.
length
-
1
인 요소를 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.