JavaScript에서 TypedArray 객체의 reverse() 함수는 타입 배열(typed array)에 저장된 요소들의 순서를 제자리(in-place)에서 반대로 뒤집는 메서드입니다. 이 함수는 별도의 매개변수를 받지 않으며, 새로운 배열을 생성하는 대신 호출된 원본 배열 자체를 직접 수정하고 그 결과로 변경된 배열의 참조를 반환합니다.
구문(Syntax)
reverse() 함수의 기본 구문은 다음과 같습니다.
typedArray.reverse()
매개변수와 반환값
- 매개변수: 없음
- 반환값: 요소 순서가 뒤집힌 원본 타입 배열(동일한 참조)
예제
다음 예제는 Int32Array 타입 배열을 생성한 후, reverse() 함수를 사용하여 요소 순서를 뒤집는 과정을 보여줍니다.
<html>
<head>
<title>JavaScript TypedArray reverse Method</title>
</head>
<body>
<script type="text/javascript">
var typedArray = new Int32Array([11, 5, 13, 4, 15, 3, 17, 2, 19, 8]);
document.write("Contents of the typed array: " + typedArray);
document.write("<br>");
var result = typedArray.reverse();
document.write("Contents of the reversed array: " + result);
</script>
</body>
</html>실행 결과
Contents of the typed array: 11,5,13,4,15,3,17,2,19,8 Contents of the reversed array: 8,19,2,17,3,15,4,13,5,11
참고 사항
reverse() 함수는 Int8Array, Uint8Array, Float32Array 등 모든 종류의 타입 배열에서 동일하게 작동합니다. 또한 새로운 배열을 복사해 만들지 않고 기존 배열을 직접 수정하기 때문에 메모리 측면에서 효율적이며, 대용량 수치 데이터를 다룰 때 유용하게 활용할 수 있습니다.