1

I was testing Object spread operator and wrote these code, but actually could not figure out how the object user is transferred into an Array. So i want to understand the flow of code. Thanks

 const user = {
   name:"JohnDoe",
   age:25
  }

function test(...user){

  console.log('isArray ', Array.isArray(user));  // true
  console.log(user);                             // Array [ {…} ]

}
test(user);
Dodz
  • 187
  • 1
  • 6
  • 14
  • It's polyfill would be like `arr= []; arr.push({Object.keys[0]:Object.values[0]})` – Meet Zaveri Jul 23 '18 at 11:50
  • 1
    Pass a second parameter to `test(user)` and see what happens. – zzzzBov Jul 23 '18 at 11:51
  • 1
    Here's a good post called "[spread is not an operator](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508)" which explains the confusion with this symbol. What you're referring to is actually a [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters). – CodingIntrigue Jul 23 '18 at 11:51
  • 1
    You’re not using object spread here, but [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) – Ben West Jul 23 '18 at 11:52
  • 1
    @BenWest Thank you that really helped :)) – Dodz Jul 23 '18 at 12:00
  • @CodingIntrigue Thank you that really helped :)) – Dodz Jul 23 '18 at 12:00

0 Answers0