You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart语言中列表场景下的'...'运算符是什么及作用?

JavaScript中...扩展运算符的作用解释

你看到的...是JavaScript ES6引入的扩展运算符,在var list = [...otherList];这段代码里,它的核心作用是将otherList这个可迭代对象(这里是数组)的所有元素逐个展开,然后填充到新创建的list数组中,本质是给原数组生成一个浅拷贝。

举个实际例子就能明白:

const otherList = [1, 2, { name: 'test' }];
const list = [...otherList];

console.log(list); // 输出: [1, 2, { name: 'test' }]
console.log(list === otherList); // 输出: false,说明是全新的数组对象

需要注意的是,这是浅拷贝——如果原数组里包含引用类型(比如对象、嵌套数组),新数组里的对应元素只是原引用的副本,修改这类元素的内部属性会同步影响原数组:

list[2].name = 'updated';
console.log(otherList[2].name); // 输出: 'updated'

扩展运算符还有不少实用场景:

  • 合并多个数组:const combinedArr = [...arr1, ...arr2, 'newItem'];
  • 快速将类数组对象转为真正的数组:const arr = [...document.querySelectorAll('div')];
  • 函数调用时展开数组作为参数:
    function add(a, b, c) {
      return a + b + c;
    }
    add(...[1, 2, 3]); // 等价于 add(1,2,3),返回6
    

内容的提问来源于stack exchange,提问作者Scorb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 10:30:51