arrayBuffer.byteLength是JavaScript中的一個屬性,該屬性返回ArrayBuffer的長度(以字節為單位)。 ArrayBuffer是一個對象,用於表示固定長度的二進製數據。 JavaScript中屬性和函數之間的區別。 JavaScript中的屬性不過是一個值,而方法是一個函數,可以通過下麵給出的示例來理解。
// car is an object.
var car = {};
// car.name is a property of the given object.
car.name = "Audi",
// car.sayModel is a function of the given object.
car.sayModel = function() {
console.log("A8");
}
// printing property value.
console.log(car.name);
// printing function called value.
console.log(car.sayModel());
輸出:
> "Audi" > "A8"
在這裏,我們可以看到對象car的屬性將字符串存儲為“Audi”,並且可以使用car.name對其進行訪問。
sayModel是一種方法,即對象的函數,可以使用car.sayModel()進行訪問。
可以注意到,sayModel隻是使用()的函數。
字節長度和長度屬性之間的區別-
Length屬性返回String對象的長度,即字符串對象中的字符數。
例:
Input:- geeksforgeeks Output:- 13
Bytelength屬性返回ArrayBuffer的長度(以字節為單位)。 ArrayBuffer是一個對象,用於表示固定長度的二進製數據。
例:
Input:- ArrayBuffer(2) Output:- 2
用法:
arraybuffer.byteLength
- 這裏沒有任何參數作為參數,因為這是屬性而不是函數。
- 它以字節為單位返回ArrayBuffer的長度。
參數:
返回值:
讓我們看一下有關此屬性的JavaScript程序:
- 代碼1:
// ArrayBuffer is created with // some size in bytes. var A = new ArrayBuffer(2); // Here the byteLength property // is used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer in bytes is printed. console.log(B);
輸出:
> 2
- 代碼2:
// ArrayBuffer is created // with some size in bytes. var A = new ArrayBuffer(0); // Here the byteLength property // is used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer // in bytes is printed. console.log(B);
輸出:
> 0
- 代碼3:
// ArrayBuffer is created // with some size in bytes. var A = new ArrayBuffer("geeksforgeeks"); // Here the byteLength property is // used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer // in bytes is printed. console.log(B);
輸出:
> 0
- 代碼4:
// ArrayBuffer is created // with some size in bytes. var A = new ArrayBuffer(4.6); // Here the byteLength property // is used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer // in bytes is printed. console.log(B);
輸出:
> 4
- 不能取負數,因為字節大小隻能取正整數值,包括零。
// ArrayBuffer is created // with some size in bytes. var A = new ArrayBuffer(-2); // Here the byteLength property is // used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer // in bytes is printed. console.log(B);
輸出:
Error:Invalid array buffer length
- 不能采用複數,因為以字節為單位的大小隻能采用正整數值,包括零。
// ArrayBuffer is created with // some size in bytes. var A = new ArrayBuffer(2 + 4i); // Here the byteLength property // is used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer // in bytes is printed. console.log(B);
輸出:
Error:Invalid or unexpected token
- 以字節為單位的大小必須小於否則返回錯誤:無效的數組緩衝區長度。
// ArrayBuffer is created with some size in bytes. var A = new ArrayBuffer(2338945720394852703948572); // Here the byteLength property is // used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer in bytes is printed. console.log(B);
輸出:
Error:Invalid array buffer length
與該函數相關的錯誤和異常:
應用:
它的應用是獲取ArrayBuffer的字節長度。
- 代碼1:
// ArrayBuffer is created // with some size in bytes. var A = new ArrayBuffer(23); // Here the byteLength property // is used for checking the size. var B = A.byteLength; // Length of the ArrayBuffer // in bytes is printed. console.log(B);
輸出:
> 23
相關用法
- Javascript Math.LN2用法及代碼示例
- javascript Error name用法及代碼示例
- Javascript Math.PI用法及代碼示例
- javascript MouseEvent which用法及代碼示例
- javascript MouseEvent ctrlKey用法及代碼示例
- javascript MouseEvent shiftKey用法及代碼示例
- Javascript Math.LOG2E用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | arrayBuffer.byteLength property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。