+ 1

Object.assign() in ES6 (What is the output of this code?)

What is the output of this code? const obj1 = { a: 0, b: 2, c: 4 }; const obj2 = Object.assign({c: 5, d: 6}, obj1); console.log(obj2.c, obj2.d);

29th Oct 2021, 4:59 PM
Kniffin Haruka
Kniffin Haruka - avatar
5 ответов
+ 3
https://code.sololearn.com/WKPdKnn9WC2a the output of this code is : 4 6
29th Oct 2021, 5:16 PM
SoloProg
SoloProg - avatar
+ 3
The assign() copies key-value pairs into a new array. d has a value of 6, and c has a value of 5 from the first argument, however, c is later rewritten as 4, from second argument obj1 That's why the console logs "4 6" instead of "5 6"
30th Oct 2021, 12:36 AM
Gordon
Gordon - avatar
+ 1
46 is the accepted answer, but should be 4 6
11th Jul 2022, 3:24 PM
Francisca Palma
Francisca Palma - avatar
+ 1
46
26th Aug 2022, 7:26 PM
Nidhal Labri
Nidhal Labri - avatar
- 1
4 6
27th Jun 2022, 6:25 AM
Bexzod
Bexzod - avatar