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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。