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 8, 2025 12:52
下一篇 Mar 8, 2025 12:52

Related Posts

  • [Opening]

    I am Walker, born in the early 1980s, a journeyer through code and life. A full-stack development engineer, I navigate the boundaries between front-end and back-end, dedicated to the intersection of technology and art. Code is the language with which I weave dreams; projects are the canvas on which I paint the future. Amidst the rhythmic tapping of the keyboard, I explore the endless possibilities of technology, allowing inspiration to bloom eternally within the code. An avid coffee enthusiast, I am captivated by the poetry and ritual of every pour-over. In the rich aroma and subtle bitterness of coffee, I find focus and inspiration, mirroring my pursuit of excellence and balance in the world of development. Cycling...

    Feb 6, 2025 Personal
    2.2K00
  • 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.2K00
  • Waving to the world, embracing infinite possibilities 🌍✨

    Standing higher, seeing further. Life is like a series of tall buildings; we constantly climb upwards, not to show off the height, but to see a broader landscape. The two girls in the picture stand atop the city, with outstretched arms, as if embracing the boundless possibilities of the world. This is not merely a journey overlooking the city, but rather, a tribute to freedom and dreams. Brave Exploration, Breaking Boundaries. Everyone's life is an adventure; we are born free, and thus should explore unknown landscapes and experience more stories. Perhaps there will be challenges along the way, but it is precisely those moments of ascent...

    Personal Feb 26, 2025
    1.3K00
  • In-depth Understanding of ES6 001 [Study Notes]

    Block-Level Scope Binding
    Previously, `var` variable declarations, regardless of where they were declared, were considered to be declared at the top of their scope. Since functions are first-class citizens, the typical order was `function functionName()`, followed by `var variable`.

    Block-Level Declarations
    Block-level declarations are used to declare variables that cannot be accessed outside the scope of a specified block. Block-level scope exists in:
    - Inside functions
    - Within blocks (the region between `{` and `}`)

    Temporal Dead Zone
    When the JavaScript engine scans code and finds variable declarations, it either hoists them to the top of the scope...

    Personal Mar 8, 2025
    1.6K00
  • Go Engineering Systematic Course 003 [Study Notes]

    grpc grpc grpc-go grpc seamlessly integrates protobuf protobuf. For those of you accustomed to using JSON and XML data storage formats, I believe most have never heard of Protocol Buffer. Protocol Buffer is actually a lightweight & efficient structured data storage format developed by Google, and its performance is truly much, much stronger than JSON and XML! protobuf…

    Personal Nov 25, 2025
    18800
EN
简体中文 繁體中文 English
欢迎🌹 Coding never stops, keep learning! 💡💻 光临🌹