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

(0)
Walker的头像Walker
上一篇 Nov 25, 2025 16:00
下一篇 Nov 25, 2025 15:00

Related Posts

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

    Extending object functionality
    Ordinary objects: objects that possess all default internal behaviors of a JavaScript object.
    Exotic objects: objects that possess certain internal behaviors that deviate from the default.
    Standard objects: objects defined in the ES6 specification, such as Array/Date.
    Built-in objects: objects that exist in the JavaScript execution environment when the script begins execution; all standard objects are built-in objects.
    Object literal syntax extensions:
    Shorthand for property initializers: when an object's property has the same name as a local variable, there's no need to write the colon and value.
    Shorthand syntax for object methods...

    Personal Mar 8, 2025
    1.4K00
  • TS Everest 003 [Study Notes]

    Decorator // Decorator // Can only be used in classes (on the class itself, or class members) // Decorators, class decorators, property decorators, accessor decorators, parameter decorators //
    1. Type Decorator: Used to extend a class, or can also return a subclass. //
    First, `experimentalDecorators` must be enabled in `tsconfig.json`. `const classDecorator1 =

    Personal Mar 27, 2025
    1.5K00
  • 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
    32700
  • 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
  • Node: In-depth Yet Easy to Understand (Shengsi Garden Education) 002 [Study Notes]

    Node's package management and loading mechanisms: npm search xxx, npm view xxx, npm install xxx. Node.js file system operation APIs: Node.js's `fs` module provides synchronous (Sync) and callback/Promise-based asynchronous APIs for operating on local files and directories. Commonly used capabilities in daily development include reading, writing, appending, deleting, traversing directories, listening for changes, and so on. The following examples are based on C...

    Personal Nov 24, 2025
    34600
EN
简体中文 繁體中文 English