Deep Dive into ES6 010 [Study Notes]

Improved array functionality. The peculiar behavior of `new Array()`: when a single numeric value is passed to the constructor, the array's `length` property is set to that value; if multiple values are passed, regardless of whether they are numeric or not, they all become elements of the array. This behavior is confusing, as it's not always possible to pay attention to the type of data passed in, thus posing a certain risk. `Array.of()`, regardless of how many arguments are passed, has no special case for a single numeric value (one argument and numeric type); it always returns an array containing all arguments...

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://www.walker-learn.xyz/archives/4336

(0)
Walker的头像Walker
上一篇 Mar 8, 2025 12:52
下一篇 Mar 8, 2025 12:52

Related Posts

  • In-depth Understanding of ES6 005 [Study Notes]

    Destructuring: Making data access more convenient. If you declare variables using `var`, `let`, or `const` with destructuring, you must provide an initializer (i.e., the value on the right side of the equals sign). The following will cause an error:
    // Syntax error `var {tyep,name}`
    // Syntax error `let {type,name}`
    // Syntax error `const {type,name}`
    To assign values to already declared variables using destructuring, consider the following:
    `let node = { type:&qu...`

    Personal Mar 8, 2025
    1.3K00
  • Go Engineer Training Course 018 [Learning Notes]

    Getting Started with API Gateway and Continuous Deployment (Kong & Jenkins) corresponds to the course materials "Chapter 2: Getting Started with Jenkins" and "Chapter 3: Deploying Services with Jenkins", outlining the practical path for Kong and Jenkins in enterprise-level continuous delivery. Even with zero prior experience, you can follow the steps to build your own gateway + continuous deployment pipeline. Pre-class Introduction: What is an API Gateway? An API Gateway sits between clients and backend microservices...

    Personal Nov 25, 2025
    26400
  • Go Engineer Comprehensive Course: protoc-gen-validate Study Notes

    protoc-gen-validate: Introduction and Usage Guide ✅ What is protoc-gen-validate? protoc-gen-validate (PGV for short) is a Protocol Buffers plugin used to add validation logic for struct fields in generated Go code. It automatically generates validation code for each field by adding validation rules in .proto files, saving you the trouble of manually...

    Personal Nov 25, 2025
    1.4K00
  • In-depth Understanding of ES6 009 [Learning Notes]

    Classes in JavaScript function PersonType(name){ this.name = name; } PersonType.prototype.sayName = function(){ console.log(this.name) } var person = new PersonType("Nicholas") p…

    Personal Mar 8, 2025
    1.3K00
  • Go Engineer System Course 012 [Study Notes]

    Integrate Elasticsearch in Go 1. Client Library Selection 1.1 Mainstream Go ES Clients olivere/elastic: Most comprehensive features, elegant API design, supports ES 7.x/8.x elastic/go-elasticsearch: Official client, lightweight, closer to native REST API go-elasticsearch/elasticsearch: Community-maintained offi…

    Personal Nov 25, 2025
    29400
EN
简体中文 繁體中文 English