From 8d1313c84cc82d81363ed62b75baedb9a65ff2e3 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:08:24 +0800 Subject: remove font tag in zh-CN --- .../reference/operators/spread_syntax/index.html | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/operators/spread_syntax') diff --git a/files/zh-cn/web/javascript/reference/operators/spread_syntax/index.html b/files/zh-cn/web/javascript/reference/operators/spread_syntax/index.html index aaa1544a4c..0397ee8e96 100644 --- a/files/zh-cn/web/javascript/reference/operators/spread_syntax/index.html +++ b/files/zh-cn/web/javascript/reference/operators/spread_syntax/index.html @@ -16,13 +16,13 @@ translation_of: Web/JavaScript/Reference/Operators/Spread_syntax -

语法

+

语法

-

函数调用:

+

函数调用:

myFunction(...iterableObj);
-

字面量数组构造或字符串:

+

字面量数组构造或字符串:

[...iterableObj, '4', ...'hello', 6];
@@ -30,25 +30,25 @@ translation_of: Web/JavaScript/Reference/Operators/Spread_syntax
let objClone = { ...obj };
-

示例

+

示例

在函数调用时使用展开语法

等价于apply的方式

-

如果想将数组元素迭代为函数参数,一般使用{{jsxref( "Function.prototype.apply")}} 的方式进行调用

+

如果想将数组元素迭代为函数参数,一般使用{{jsxref( "Function.prototype.apply")}} 的方式进行调用。

function myFunction(x, y, z) { }
 var args = [0, 1, 2];
 myFunction.apply(null, args);
-

有了展开语法,可以这样写:

+

有了展开语法,可以这样写:

function myFunction(x, y, z) { }
 var args = [0, 1, 2];
 myFunction(...args);
-

所有参数都可以通过展开语法来传值,也不限制多次使用展开语法。

+

所有参数都可以通过展开语法来传值,也不限制多次使用展开语法。

function myFunction(v, w, x, y, z) { }
 var args = [0, 1];
@@ -56,13 +56,13 @@ myFunction(-1, ...args, 2, ...[3]);

在 new 表达式中应用

-

使用 new 关键字来调用构造函数时,不能直接使用数组+ apply 的方式(apply 执行的是调用 [[Call]] , 而不是构造 [[Construct]])。当然, 有了展开语法, 将数组展开为构造函数的参数就很简单了:

+

使用 new 关键字来调用构造函数时,不能直接使用数组+ apply 的方式(apply 执行的是调用 [[Call]] , 而不是构造 [[Construct]])。当然, 有了展开语法, 将数组展开为构造函数的参数就很简单了:

-
var dateFields = [1970, 0, 1]; // 1970年1月1日
+
var dateFields = [1970, 0, 1]; // 1970年1月1日
 var d = new Date(...dateFields);
 
-

如果不使用展开语法, 想将数组元素传给构造函数, 实现方式可能是这样的

+

如果不使用展开语法, 想将数组元素传给构造函数, 实现方式可能是这样的:

function applyAndNew(constructor, args) {
    function partial () {
@@ -90,13 +90,13 @@ console.log(new myConstructorWithArguments);
 // (myConstructor构造函数中):           ["hi", "how", "are", "you", "mr", null]
 // ("new myConstructorWithArguments"中): {prop1: "val1", prop2: "val2"}
-

构造字面量数组时使用展开语法

+

构造字面量数组时使用展开语法

-

构造字面量数组时更给力!

+

构造字面量数组时更给力!

-

没有展开语法的时候,只能组合使用 push, splice, concat 等方法,来将已有数组元素变成新数组的一部分。有了展开语法,  通过字面量方式, 构造新数组会变得更简单、更优雅:

+

没有展开语法的时候,只能组合使用 push, splice, concat 等方法,来将已有数组元素变成新数组的一部分。有了展开语法,  通过字面量方式, 构造新数组会变得更简单、更优雅:

-
var parts = ['shoulders', 'knees'];
+
var parts = ['shoulders', 'knees'];
 var lyrics = ['head', ...parts, 'and', 'toes']; 
 // ["head", "shoulders", "knees", "and", "toes"]
 
@@ -151,7 +151,7 @@ var arr2 = [3, 4, 5]; arr1 = [...arr2, ...arr1]; // arr1 现在为 [3, 4, 5, 0, 1, 2]
-

构造字面量对象时使用展开语法

+

构造字面量对象时使用展开语法

Rest/Spread Properties for ECMAScript 提议(stage 4) 对 字面量对象 增加了展开特性。其行为是, 将已有对象的所有可枚举(enumerable)属性拷贝到新构造的对象中.

@@ -232,12 +232,12 @@ var array = [...obj]; // TypeError: obj is not iterable -

浏览器兼容性

+

浏览器兼容性

{{Compat("javascript.operators.spread")}}

相关链接

-- cgit v1.2.3-54-g00ecf