From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../global_objects/dataview/dataview/index.html | 75 +++++++++ .../reference/global_objects/dataview/index.html | 168 +++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 files/ko/web/javascript/reference/global_objects/dataview/dataview/index.html create mode 100644 files/ko/web/javascript/reference/global_objects/dataview/index.html (limited to 'files/ko/web/javascript/reference/global_objects/dataview') diff --git a/files/ko/web/javascript/reference/global_objects/dataview/dataview/index.html b/files/ko/web/javascript/reference/global_objects/dataview/dataview/index.html new file mode 100644 index 0000000000..7c8b3634a1 --- /dev/null +++ b/files/ko/web/javascript/reference/global_objects/dataview/dataview/index.html @@ -0,0 +1,75 @@ +--- +title: DataView() 생성자 +slug: Web/JavaScript/Reference/Global_Objects/DataView/DataView +tags: + - Constructor + - DataView + - JavaScript + - Reference + - TypedArrays +translation_of: Web/JavaScript/Reference/Global_Objects/DataView/DataView +--- +
{{JSRef}}
+ +

DataView() 생성자는 새로운 {{jsxref("DataView")}} 객체를 생성합니다.

+ +
{{EmbedInteractiveExample("pages/js/dataview-constructor.html")}}
+ + + +

구문

+ +
new DataView(buffer [, byteOffset [, byteLength]])
+ +

매개변수

+ +
+
buffer
+
새로운 DataView 객체의 저장소로 사용할 {{jsxref("ArrayBuffer")}} 또는 {{jsxref("SharedArrayBuffer")}} {{experimental_inline}}.
+
byteOffset {{optional_inline}}
+
새로운 뷰가 참조할 첫 번째 바이트로의 바이트 단위 오프셋. 지정하지 않을 경우 버퍼 뷰는 첫 번째 바이트부터 시작합니다.
+
byteLength {{optional_inline}}
+
바이트 배열의 요소 수. 지정하지 않을 경우 뷰의 길이는 버퍼의 길이와 같아집니다.
+
+ +

반환 값

+ +

지정한 데이터 버퍼를 나타내는 새로운 DataView 객체.

+ +

DataView 객체는 배열 버퍼의 "인터프리터"라고 생각하면 좀 더 쉽습니다. DataView는 읽기와 쓰기 모두에서 버퍼에 잘 맞도록 숫자를 올바르게 변환하는 법, 즉 정수/부동소수점 실수 변환, 엔디언 등 이진 형식으로 나타낸 숫자의 처리법을 알고 있습니다.

+ +

예외

+ +
+
{{jsxref("RangeError")}}
+
+

byteOffset 또는 byteLength 매개변수가 버퍼의 끝을 벗어남.

+ +

예를 들어, 버퍼가 16바이트 길이인데 byteOffset을 8로, byteLength를 10으로 설정할 경우 총 길이 18로서 2바이트를 초과하므로 오류가 발생합니다.

+
+
+ +

명세

+ + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-dataview-constructor', 'DataView constructor')}}
+ +

브라우저 호환성

+ + + +

{{Compat("javascript.builtins.DataView.DataView")}}

+ +

같이 보기

+ + diff --git a/files/ko/web/javascript/reference/global_objects/dataview/index.html b/files/ko/web/javascript/reference/global_objects/dataview/index.html new file mode 100644 index 0000000000..3bde0a032c --- /dev/null +++ b/files/ko/web/javascript/reference/global_objects/dataview/index.html @@ -0,0 +1,168 @@ +--- +title: DataView +slug: Web/JavaScript/Reference/Global_Objects/DataView +tags: + - DataView + - JavaScript + - Reference + - TypedArrays +translation_of: Web/JavaScript/Reference/Global_Objects/DataView +--- +
{{JSRef}}
+ +

DataView 뷰는 플랫폼의 자체 {{glossary("endianness", "엔디언")}}(바이트 정렬 방법)에 신경 쓰지 않으면서 {{jsxref("ArrayBuffer")}}에서 다양한 숫자 자료형의 데이터를 읽고 쓰기 위한 저수준 인터페이스를 제공합니다.

+ +

설명

+ +

엔디언

+ +

다중 바이트 숫자 자료형은 장치의 아키텍처에 따라 메모리에 다르게 표현됩니다. {{glossary("endianness", "엔디언")}} 용어 사전을 참고하세요. DataView 접근자는 코드를 실행하는 컴퓨터의 엔디언에 관계 없이 데이터의 접근법을 명시적으로 통제할 수 있습니다.

+ +
var littleEndian = (function() {
+  var buffer = new ArrayBuffer(2);
+  new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
+  // Int16Array uses the platform's endianness.
+  return new Int16Array(buffer)[0] === 256;
+})();
+console.log(littleEndian); // true or false
+ +

64비트 정수 자료형

+ +

JavaScript는 64비트 정수형 값에 대한 표준 지원을 아직 제공하지 않으므로 DataView도 64비트 연산을 지원하지 않습니다. 필요한 경우 최대 정확도 {{jsxref("Number.MAX_SAFE_INTEGER")}}의 값을 가져올 수 있는 getUint64() 함수를 만들 수는 있으며, 어느 정도까지는 충분할 것입니다.

+ +
function getUint64(dataview, byteOffset, littleEndian) {
+  // split 64-bit number into two 32-bit (4-byte) parts
+  const left =  dataview.getUint32(byteOffset, littleEndian);
+  const right = dataview.getUint32(byteOffset+4, littleEndian);
+
+  // combine the two 32-bit values
+  const combined = littleEndian? left + 2**32*right : 2**32*left + right;
+
+  if (!Number.isSafeInteger(combined))
+    console.warn(combined, 'exceeds MAX_SAFE_INTEGER. Precision may be lost');
+
+  return combined;
+}
+ +

완전한 범위의 64비트를 지원해야 할 경우 {{jsxref("BigInt")}}를 사용하세요. 단, BigInt는 사용자 계층 라이브러리 구현체보다는 훨씬 빠르지만, 가변 크기라는 성질로 인해 JavaScript 32비트 정수보다는 항상 느릴 수밖에 없습니다.

+ +
const BigInt = window.BigInt, bigThirtyTwo = BigInt(32), bigZero = BigInt(0);
+function getUint64BigInt(dataview, byteOffset, littleEndian) {
+  // split 64-bit number into two 32-bit (4-byte) parts
+  const left = BigInt(dataview.getUint32(byteOffset|0, !!littleEndian)>>>0);
+  const right = BigInt(dataview.getUint32((byteOffset|0) + 4|0, !!littleEndian)>>>0);
+
+  // combine the two 32-bit values and return
+  return littleEndian ? (right<<bigThirtyTwo)|left : (left<<bigThirtyTwo)|right;
+}
+ +

생성자

+ +
+
{{jsxref("DataView.DataView", "DataView()")}}
+
새로운 DataView 객체를 생성합니다.
+
+ +

속성

+ +
+
DataView.prototype.constructor
+
객체의 프로토타입을 생성한 함수를 나타냅니다. 초깃값은 내장 DataView 생성자입니다.
+
{{jsxref("DataView.prototype.buffer")}} {{readonlyInline}}
+
뷰가 참조하는 {{jsxref("ArrayBuffer")}}입니다.
+
{{jsxref("DataView.prototype.byteLength")}} {{readonlyInline}}
+
뷰의 시작점부터 측정한 {{jsxref("ArrayBuffer")}}의 길이(바이트 단위)입니다.
+
{{jsxref("DataView.prototype.byteOffset")}} {{readonlyInline}}
+
뷰의 시작점 오프셋(바이트 단위)입니다.
+
+ +

메서드

+ +

읽기

+ +
+
{{jsxref("DataView.prototype.getInt8()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 8비트 정수(byte)를 가져옵니다.
+
{{jsxref("DataView.prototype.getUint8()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 8비트 부호 없는 정수(unsigned byte)를 가져옵니다.
+
{{jsxref("DataView.prototype.getInt16()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 16비트 정수(short)를 가져옵니다.
+
{{jsxref("DataView.prototype.getUint16()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 16비트 부호 없는 정수(unsigned short)를 가져옵니다.
+
{{jsxref("DataView.prototype.getInt32()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 32비트 정수(long)를 가져옵니다.
+
{{jsxref("DataView.prototype.getUint32()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 32비트 부호 없는 정수(unsigned short)를 가져옵니다.
+
{{jsxref("DataView.prototype.getFloat32()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 32비트 실수(float)를 가져옵니다.
+
{{jsxref("DataView.prototype.getFloat64()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 64비트 실수(double)를 가져옵니다.
+
{{jsxref("DataView.prototype.getBigInt64()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 64비트 정수(long long)를 가져옵니다.
+
{{jsxref("DataView.prototype.getBigUint64()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치의 64비트 부호 없는 정수(unsigned long long)를 가져옵니다.
+
+ +

쓰기

+ +
+
{{jsxref("DataView.prototype.setInt8()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 8비트 정수(byte)를 저장합니다.
+
{{jsxref("DataView.prototype.setUint8()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 8비트 부호 없는 정수(unsigned byte)를 저장합니다.
+
{{jsxref("DataView.prototype.setInt16()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 16비트 정수(short)를 저장합니다.
+
{{jsxref("DataView.prototype.setUint16()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 16비트 부호 없는 정수(unsigned short)를 저장합니다.
+
{{jsxref("DataView.prototype.setInt32()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 32비트 정수(long)를 저장합니다.
+
{{jsxref("DataView.prototype.setUint32()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 32비트 부호 없는 정수(unsigned long)를 저장합니다.
+
{{jsxref("DataView.prototype.setFloat32()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 32비트 실수(float)를 저장합니다.
+
{{jsxref("DataView.prototype.setFloat64()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 64비트 실수(double)를 저장합니다.
+
{{jsxref("DataView.prototype.setBigInt64()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 64비트 정수(long long)를 저장합니다.
+
{{jsxref("DataView.prototype.setBigUint64()")}}
+
뷰의 시작점에서부터 주어진 오프셋을 적용한 위치에 64비트 부호 없는 정수(unsigned long long)를 저장합니다.
+
+ +

예제

+ +
var buffer = new ArrayBuffer(16);
+var view = new DataView(buffer, 0);
+
+view.setInt16(1, 42);
+view.getInt16(1); //42
+
+ +

명세

+ + + + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-dataview-objects', 'DataView')}}
+ +

브라우저 호환성

+ + + +

{{Compat("javascript.builtins.DataView")}}

+ +

같이 보기

+ + -- cgit v1.2.3-54-g00ecf