JavaScript,第一行是错误,第二行是正确的。
console.log(...undefined) // error console.log({...undefined}) // {}
发布于 2017-04-27 14:41:49
console.log(...undefined) // error是一个标准的ES6扩展,它要求参数是可迭代类型。undefined是不可迭代的,因此你会得到一个错误。
console.log({...undefined})是建议的对象扩展语法。对于此语法,传入的参数将其属性复制到一个新对象中。在本例中,the spec defines the following
如果为undefined或null,则将
这就是为什么。在本例中,它将undefined视为“不复制任何内容”,因此这不是一个错误的情况。
发布于 2017-04-27 14:49:47
在不定义babel的情况下,可以将undefined定义为对象或rest参数
"use strict";
const fn = (...undefined) =>
console.log(...undefined);
fn();
fn({b: 7});
fn({g: 9, x: 10});
fn({opts: "busted"})
定义babel的位置,使用对象rest
"use strict";
const fn = ({...undefined}) =>
console.log({...undefined});
fn();
fn({b: 7});
fn({g: 9, x: 10});
fn({opts: "busted"})
尝试在定义了babel且扩展元素位于undefined之前时重现错误
"use strict";
const fn = ({...undefined}) =>
console.log(...undefined); // no error
fn();
fn({b: 7});
fn({g: 9, x: 10});
fn({opts: "busted"})
https://stackoverflow.com/questions/43649989
复制相似问题