BlogBlog
首页
  • Vue
  • TypeScript
  • React
  • Angular
  • Node.js
  • 小程序
  • Flutter
  • 数据产品
  • 大数据

    • Hadoop
    • Hive
    • Spark
  • MySQL
  • Redis
  • Java
  • Python
  • Golang
GitHub
首页
  • Vue
  • TypeScript
  • React
  • Angular
  • Node.js
  • 小程序
  • Flutter
  • 数据产品
  • 大数据

    • Hadoop
    • Hive
    • Spark
  • MySQL
  • Redis
  • Java
  • Python
  • Golang
GitHub

扩展运算符

ES6 提供了一个新的运算符,称为展开运算符,它由三个点组成 (...) 展开运算符允许您展开可迭代对象的元素,例如 数组、映射 或 集合。

展开运算符(...)是一个在ES6中引入的语法糖,它允许您将数组或对象中的元素展开到某个地方。

合并数组

const odd = [1, 3, 5];
const combined = [2, 4, ...odd, 6];
console.log(combined); // [2, 4, 1, 3, 5, 6]

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const mergedArray = [...arr1, ...arr2];
console.log(mergedArray); // 输出: [1, 2, 3, 4, 5, 6]


// 位于 odd 数组前面的三个点 ( ...) 是展开运算符。 展开运算符 (...) 解包 odd 数组的元素。

函数调用时传递数组作为参数:

function sum(a, b, c) {
  return a + b + c;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers)); // 输出: 6

对象属性展开

const obj1 = { foo: 'bar', x: 42 };
const obj2 = { foo: 'baz', y: 13 };
const clonedObj = { ...obj1 };
console.log(clonedObj); // 输出: { foo: 'bar', x: 42 }

apply() 方法

apply()是函数原型上的一个方法,它允许您调用一个函数,并指定该函数的this值以及作为数组(或类数组对象)提供的参数。

指定this上下文

function greet() {
  console.log(`Hello, my name is ${this.name}`);
}
const person = { name: 'Alice' };
greet.apply(person); // 输出: Hello, my name is Alice

传递数组作为参数

function sum(a, b, c) {
  return a + b + c;
}
const numbers = [1, 2, 3];
console.log(sum.apply(null, numbers)); // 输出: 6

展开运算符和 apply() 方法 比较与总结

特性展开运算符 (...)apply() 方法
引入版本ES6ES3
主要用途展开数组或对象调用函数并设置this值,传递数组作为参数
语法简洁性非常简洁,易于使用相对复杂,需要调用函数的方法
适用场景数组/对象合并,函数调用函数调用,特别是需要设置this值时

使用数组的 push() 方法的更好方法示例

let rivers = ['Nile', 'Ganges', 'Yangte'];
let moreRivers = ['Danube', 'Amazon'];

[].push.apply(rivers, moreRivers);
console.log(rivers);

// 等效于
rivers.push(...moreRivers);

JavaScript 展开运算符和数组操作

  1. 构建数组文字
let initialChars = ['A', 'B'];
let chars = [...initialChars, 'C', 'D'];
console.log(chars); // ["A", "B", "C", "D"]
  1. 连接数组
let numbers = [1, 2];
let moreNumbers = [3, 4];
let allNumbers = [...numbers, ...moreNumbers];
console.log(allNumbers); // [1, 2, 3, 4]
  1. 复制数组
let scores = [80, 70, 90];
let copiedScores = [...scores];
console.log(copiedScores); // [80, 70, 90]

JavaScript 展开运算符和字符串

let chars = ['A', ...'BC', 'D'];
console.log(chars); // ["A", "B", "C", "D"]
//在此示例中,我们从单个字符串构造了 chars 数组。 当我们将展开运算符应用于 'BC' 字符串时,它将 'BC' 字符串的每个字符展开到单个字符中。
最近更新:: 2025/4/17 16:24
Contributors: alice