为什么扩展语法将我的字符串转换为数组? [英] Why does spread syntax convert my string into an array?

查看:101
本文介绍了为什么扩展语法将我的字符串转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么扩展语法会将我的字符串转换为数组?

Why does spread syntax convert my string into an array?

var v = 'hello';
var [, ...w] = v; // ["e", "l", "l", "o"]

为什么是 w 不是字符串?

推荐答案

传播语法(实际上是一个标点符号,如下所示 RobG )允许迭代将扩展成更小的位。由于字符串是可迭代的(它们是内部的字符数组,更具体地是有序的整数表示字符的序列),因此它们可以扩展为单个字符。

Spread syntax (actually a punctuator as noted by RobG) allows for iterables to be spread into smaller bits. Since strings are iterables (they're character arrays internally, more specifically ordered sequences of integers representing characters), they can be spread into individual characters.

接着,解构分配以解压缩并对扩展值进行分组。由于您使用省略了字符数组的第一个元素,并且没有分配引用,它会丢失,并且可迭代对象的 rest 保存到 w 中,传播到它的各个部分,字符数组的单个字符。

Next, destructuring assignment is performed on the array to unpack and group the spread values. Since you ommit the first element of the character array with , and don't assign a reference, it's lost, and the rest of the iterable object is saved into w, spread into it's individual parts, single characters of the character array.

此操作的特定语义在> ECMAScript 2015规范 ArrayAssignmentPattern:[Elision opt AssignmentRestElement] 生产:

The specific semantics of this operation are defined in the ECMAScript 2015 Specification by the ArrayAssignmentPattern : [ Elisionopt AssignmentRestElement ] production:

12.14.5.2运行时语义:DestructuringAssignmentEvaluation

参数 value

[...]

ArrayAssignmentPattern:[Elision opt AssignmentRestElement]

ArrayAssignmentPattern : [ Elisionopt AssignmentRestElement ]


  1. iterator 成为 GetIterator value )。

  2. ReturnIfAbrupt iterator )。

  3. iteratorRecord 成为记录{[[iterator]]: iterator ,[[完成]]: false }。

  4. 如果 Elision 存在,那么

    a。让 status 是执行 iteratorRecord 作为参数的 Elision 的> IteratorDestructuringAssignmentEvaluation

    b。如果 status 突然完成,然后

         i。如果 iteratorRecord 。[[done]]为 false ,则返回 IteratorClose iterator status )。

         II。返回完成状态)。

  5. 结果成为执行 IteratorDestructuringAssignmentEvaluation AssignmentRestElement ,其中 iteratorRecord 作为参数。

  6. 如果 iteratorRecord 。[[done]]为 false ,则返回 IteratorClose iterator result )。

  7. 返回结果

  1. Let iterator be GetIterator(value).
  2. ReturnIfAbrupt(iterator).
  3. Let iteratorRecord be Record {[[iterator]]: iterator, [[done]]: false}.
  4. If Elision is present, then
    a. Let status be the result of performing IteratorDestructuringAssignmentEvaluation of Elision with iteratorRecord as the argument.
    b. If status is an abrupt completion, then
        i. If iteratorRecord.[[done]] is false, return IteratorClose(iterator, status).
        ii. Return Completion(status).
  5. Let result be the result of performing IteratorDestructuringAssignmentEvaluation of AssignmentRestElement with iteratorRecord as the argument.
  6. If iteratorRecord.[[done]] is false, return IteratorClose(iterator, result).
  7. Return result.


此处, Elision 指的是使用一个或多个逗号()传播时省略的元素,与名称所暗示的省略音节相比, AssignmentRestElement 指的是目标在这种情况下,接收传播和结构化值 w

Here, Elision refers to an omitted element when spreading with one or more commas (,), comparable to omitted syllables as the name suggests, and AssignmentRestElement refers to the target that will receive the spread and destructured values, w in this case.

这是什么我首先从内部 @@ iterator 方法获取对象的迭代器,然后逐步完成迭代器,跳过由 Elision指定的省略宽度指示的许多元素 IteratorDestructuringAssignmentEvaluation 中制作。一旦完成,它将逐步执行 AssignmentRestElement 生成的迭代器,并分配一个包含所有扩展值的新数组 - 这就是 w 是。它接收展开的单字符数组,解压缩以排除第一个字符。

What this does is first get the iterator of the object, from the internal @@iterator method and steps through that iterator, skipping however many elements indicated by the elision's width by the Elision production in IteratorDestructuringAssignmentEvaluation. Once that's done, it will step through the iterator of the AssignmentRestElement production, and assign a new array with all the spread values -- that's what w is. It receives the spread out single-character array, unpacked to exclude the first character.

@@ iterator 方法其中迭代来自众所周知符号和更改对象可以改变它的迭代方式,如使者的答案。具体而言, <$ c的默认实现$ c> @@ iterator String的方法如下:

The @@iterator method in which the iteration is gotten from is a well-known Symbol and changing it for an object can change how it's iterated, as in Emissary's answer. Specifically, the default implementation of the @@iterator method for String is as follows:


21.1 .3.27 String.prototype [@@ iterator]()

当调用@@ iterator方法时,它返回一个Iterator对象( 25.1.1.2 )迭代字符串值的代码点,将每个代码点作为String值返回。

When the @@iterator method is called it returns an Iterator object (25.1.1.2) that iterates over the code points of a String value, returning each code point as a String value.

因此,迭代器允许通过单个代码点进行迭代 ,或字符串的字符 - 因此传播字符串将导致其字符数组。

Thus, the iterator allows for iteration through single code points, or characters of a string -- and consequently spreading the string will result in an array of its characters.

这篇关于为什么扩展语法将我的字符串转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆