spread-syntax

Spreading undefined in array vs object

淺唱寂寞╮ 提交于 2019-11-27 02:34:00
问题 Why does spreading undefined in an object return an empty object? {...undefined} // equals {} : console.log({...undefined}) And Why does spreading undefined in an array give you an error? [...undefined] // type error : console.log([...undefined]) 回答1: As noted in the comments, and summarized by @ftor from #687, object spread is equivalent 1 to Object.assign() (issues #687, #45), where as spread in array literal context is iterable spread. Quoting Ecma-262 6.0, Object.assign() is defined as:

Using spread operator multiple times in javascript?

荒凉一梦 提交于 2019-11-26 17:52:38
Why can't spread operator be used multiple times? let arr = [[[1, 2, 3]]]; console.log(arr); // Array [ Array[1] ] console.log(...arr); // Array [ Array[3] ] console.log(...(...arr)); // SyntaxError: expected '=>' after argument list, got ')' I would expect: console.log(...(...arr)); // Array [ 1, 2, 3 ] Why can't spread operator be used multiple times? ... is not an operator. (...arr) is not valid JavaScript. ... is only allowed inside array literals and in arguments lists, but those are special forms of the syntax (notice the ... in the production rules below). ArrayLiteral ArrayLiteral : [

Spread Syntax vs Rest Parameter in ES2015 / ES6

落花浮王杯 提交于 2019-11-26 15:10:05
问题 I am confused about the spread syntax and rest parameter in ES2015. Can anybody explain the difference between them with proper examples? 回答1: When using spread, you are expanding a single variable into more: var abc = ['a', 'b', 'c']; var def = ['d', 'e', 'f']; var alpha = [ ...abc, ...def ]; console.log(alpha)// alpha == ['a', 'b', 'c', 'd', 'e', 'f']; When using rest arguments, you are collapsing all remaining arguments of a function into one array: function sum( first, ...others ) { for (

Usage of rest parameter and spread operator in javascript

大憨熊 提交于 2019-11-26 11:04:44
问题 What\'s the usage of rest parameter that will be added in ECMAScript 6? For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element: // ES 5 store(\'Joe\', \'money\'); store(\'Jane\', \'letters\', \'certificates\'); function store(name) { var items = [].slice.call(arguments, 1); //[\'money\'] in first case items.forEach(function (item) { vault.customer[name].push(item); }); } and that will be equivalent to the following code in

Using spread operator multiple times in javascript?

南楼画角 提交于 2019-11-26 05:37:32
问题 Why can\'t spread operator be used multiple times? let arr = [[[1, 2, 3]]]; console.log(arr); // Array [ Array[1] ] console.log(...arr); // Array [ Array[3] ] console.log(...(...arr)); // SyntaxError: expected \'=>\' after argument list, got \')\' I would expect: console.log(...(...arr)); // Array [ 1, 2, 3 ] 回答1: Why can't spread operator be used multiple times? ... is not an operator. (...arr) is not valid JavaScript. ... is only allowed inside array literals and in arguments lists, but

Spread Syntax ES6

不羁岁月 提交于 2019-11-26 04:25:43
问题 Consider the following sample code var x = [\"a\", \"b\", \"c\"]; var z = [\"p\", \"q\"]; var d = [...x, ...z]; var e = x.concat(z); Here, the value of d and e are exactly same and is equal to [\"a\", \"b\", \"c\", \"p\", \"q\"] , so, What exactly is the difference between these two? Which one is more efficient and why? What exactly is the use of spread syntax? Don\'t you think the introduction of these little shortcuts in a formal vast language may leave some unnoticed bugs, I mean either it

Is …foo an operator or syntax?

ぃ、小莉子 提交于 2019-11-26 01:36:20
问题 I\'ve heard ... referred to both as \'spread syntax \' and \'the spread operator \', with the latter being a lot more popular. The URL of the relevant MDN documentation suggests that it was initially referred to as the spread operator but later changed to spread syntax, and MDN\'s list of operators doesn\'t mention it. Google seems to suggest the term operator is more popular and accepted, with sites such as the Microsoft documentation and es6-features.org referring to it as such. Which term

How to deep merge instead of shallow merge?

自作多情 提交于 2019-11-25 23:58:07
问题 Both Object.assign and Object spread only do a shallow merge. An example of the problem: // No object nesting const x = { a: 1 } const y = { b: 1 } const z = { ...x, ...y } // { a: 1, b: 1 } The output is what you\'d expect. However if I try this: // Object nesting const x = { a: { a: 1 } } const y = { a: { b: 1 } } const z = { ...x, ...y } // { a: { b: 1 } } Instead of { a: { a: 1, b: 1 } } you get { a: { b: 1 } } x is completely overwritten because the spread syntax only goes one level deep

What is SpreadElement in ECMAScript documentation? Is it the same as Spread syntax at MDN?

一曲冷凌霜 提交于 2019-11-25 22:17:41
问题 At ECMAScript specification the SpreadElement is described SpreadElement[Yield]: ...AssignmentExpression[In, ?Yield] Is this the same as the Spread syntax Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. Syntax For function calls: