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
上一篇 Mar 10, 2026 00:00
下一篇 Mar 8, 2026 15:40

Related Posts

  • Node: Demystified (Shengsi Garden Education) 001 [Learning Notes]

    Node.js: Understanding it from an Asynchronous Programming Paradigm
    Node.js's Positioning and Core Philosophy
    Based on the V8 engine + libuv event-driven library, it brings JavaScript from the browser to the server side. It uses a single-threaded event loop to handle I/O, maximizing CPU time slices while waiting for I/O, making it particularly suitable for high-concurrency, I/O-intensive scenarios. "Don't block the main thread" is its design philosophy: try to offload time-consuming operations to the kernel or a thread pool, and callback results...

    Personal Nov 24, 2025
    34300
  • TS Mount Everest 002 [Study Notes]

    Generics /* * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git &a…

    Personal Mar 27, 2025
    1.6K00
  • Go Engineer Comprehensive Course: Protobuf Guide [Study Notes]

    Protocol Buffers Getting Started Guide 1. Introduction Protocol Buffers (protobuf for short) is a language-agnostic, platform-agnostic, extensible structured data serialization mechanism developed by Google. Compared with serialization methods such as JSON and XML, protobuf is smaller, faster, and simpler. Project homepage: https://github.com/protocolbuffers/prot…

    Personal Nov 25, 2025
    1.3K00
  • 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.4K00
  • In-depth Understanding of ES6 012 [Study Notes]

    Proxy and Reflection API
    A Proxy is a wrapper that can intercept and modify underlying JavaScript engine operations. It exposes internal operational objects, enabling developers to create custom built-in objects.

    Proxy Traps
    Overridden Features | Default Behavior
    get Reads a property value | Reflect.get()
    set Writes a property value | Reflect.set()
    has `in` operator | Reflect...

    Personal Mar 8, 2025
    1.2K00
EN
简体中文 繁體中文 English