Improved Array Features
new Array()'s peculiar behavior: when a constructor is passed a `numeric value`, the array's length property will be set to that value; if multiple values are passed, regardless of whether they are numeric, they will become elements of the array. This characteristic is confusing, as you cannot always pay attention to the type of data being passed, thus posing a certain risk.
Array.of()
Regardless of how many arguments are passed, there is no special case for a single numeric value (one argument that is numeric); it always returns an array containing all arguments.
let items = Array.of(1,2);
console.log(items.length); //2
console.log(items[0]); // 1
console.log(items[1]); // 2
items = Array.of(2);
console.log(items.length); //1
console.log(items[0]); //2
items = Array.of("2");
console.log(items.length); //1
console.log(items[0]); //"2"
Array.from()
Converts array-like objects into array objects for use. Array.from() creates a new array based on the elements in the `arguments` object. `args` is an instance of `Array` containing the same values at the same positions as in the `arguments` object. For example:
function doSomthing(){
var args = Array.from(arguments);
// 使用args
}
// 可以提供一个映射函数作为Array.from()的第二个参数,这个函数用来将类数组对象中的每一个值转换成其他形式
// 还有第三个参数,是用来绑定上下文的
function translage(){
return Array.from(arguments,(value)=>value+1)
}
let numbers = translage(1,2,3)
console.log(numbers) // 2,3,4
// 可以转换可迭代对象
let numbers = {
*[Symbol.iterator](){
yield 1;
yield 2;
yield 3;
}
}
let number2 = Array.from(numbers,(value)=>value+1)
console.log(numbers2) // 2,3,4
New Methods for All Objects
- `find()` and `findIndex()` methods
- `fill()`
- `copyWithin`
Typed Arrays
A specialized array used for handling numeric type data (as its name suggests, not all types).
To use typed arrays, you must first create a data buffer.
let buffer = new ArrayBuffer(10) // 分配10个节节
console.log(buffer.byteLength) // 10
You can also use the `slice` method to split an existing array buffer to create a new one. This `slice()` method is very similar to the `slice()` method on arrays, taking a start index and an end index as arguments, and then returning a new `ArrayBuffer` instance, which consists of a slice of the original array buffer.
let buffer = new ArrayBuffer(10)
let buffer2 = buffer.slice(2,6);
console.log(buffer2.byteLength) //2
Manipulating Array Buffers via Views
First create an `ArrayBuffer` instance, and then use this instance to create a new `DataView`.
let buffer = new ArrayBuffer(10),
view = new DataView(buffer);
// view对象可以访问缓冲区中所有10个字节,如果提供一个表示比特偏移的数值,那么这个缓冲区的其中一部分来创建视图
view = new DataView(buffer,5,2); // 包含位于索引5和6的字符
- The following properties can be used to get view information:
- `buffer`: the bound array buffer
- `byteOffset`: defaults to 0
- `byteLength`
Reading and Writing Data
JavaScript has 8 numeric data types. For each of them, you can find corresponding methods on the `DataView` prototype for writing and reading data in the array buffer. These method names start with `set` or `get`, followed by the abbreviation of each data type, as follows:
- `getInt8(byteOffset,litteEndian)`
- `setInt8(byteOffset,litteEndian)`
- `getUnit8(byteOffset,litteEndian)`
- `setUnit8(byteOffset,litteEndian)`
- ...
let buffer = new ArrayBuffer(10),
view = new DataView(buffer);
view.setInt8(0,5);
view.setInt8(1,-1);
console.log(view.getInt16(0)); //1535
console.log(view.getInt8(0)); //5
console.log(view.getInt8(1)); //-1
Typed Arrays are Views
- Similarities between typed arrays and regular arrays
- Differences
主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/4336