📜  JavaScript | arrayBuffer.byteLength 属性

📅  最后修改于: 2022-05-13 01:58:10.324000             🧑  作者: Mango

JavaScript | arrayBuffer.byteLength 属性

arrayBuffer.byteLength是 JavaScript 中的一个属性,它以字节为单位返回 ArrayBuffer 的长度。 ArrayBuffer 是一个对象,用于表示固定长度的二进制数据。
javascript中属性和函数的区别。
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());


javascript
// 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);


javascript
// 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);


javascript
// 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);


javascript
// 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);


javascript
// 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);


javascript
// 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);


javascript
// 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);


javascript
// 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);


输出:

> "Audi"
> "A8"

在这里,我们可以看到对象 car 的属性将字符串存储为“Audi”,并且可以使用 car.name 访问它。
sayModel 是一个方法,即对象的一个函数,它可以通过 car.sayModel() 访问。
可以注意到sayModel只是一个使用()的函数。
bytelength 和 length 属性之间的区别-
Length 属性返回字符串对象的长度,即字符串对象中的字符数。
例子:

Input:- geeksforgeeks
Output:- 13

Bytelength 属性以字节为单位返回 ArrayBuffer 的长度。 ArrayBuffer 是一个对象,用于表示固定长度的二进制数据。
例子:

Input:- ArrayBuffer(2)
Output:- 2

句法:

arraybuffer.byteLength

让我们看看这个属性的 JavaScript 程序:

代码#1:

javascript

// 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:

javascript

// 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:

javascript

// 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:

javascript

// 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

不能取负数作为字节大小,只能取正整数值,包括零。

javascript

// 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

不能取复数,因为字节大小只能取正整数值,包括零。

javascript

// 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

字节大小必须小于2^5^3  否则返回错误:无效的数组缓冲区长度。

javascript

// 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:

javascript

// 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

支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐
  • 歌剧
  • 苹果浏览器