InterviewSolution
Saved Bookmarks
| 1. |
Compare the ES5 and ES6 codes for object initialization and parsing returned objects. |
|
Answer» Object initialization: VARIABLES with the same name are frequently used to create object PROPERTIES. Consider the following scenario: // ES5 codevar x = 1, y = 2, z = 3; ob = { x : a, y : b, z : z };// ob.x = 1, ob.y = 2, ob.z = 3In ES6, there's no need for tedious repetition! // ES6 codeconst x = 1, y = 2, z = 3; ob = { x y z };// ob.x = 1, ob.y = 2, ob.z = 3Parsing returned objects: Only one value can be returned by a function, but that value could be an object with hundreds of properties and/or methods. In ES5, you must first get the returned object and then extract values from it. Consider the following scenario: // ES5 codevar ob = getObject(), a = ob.a, b = ob.b, c = ob.c;This is MADE easier by ES6 destructuring, which ELIMINATES the need to keep the object as a variable: // ES6 codeconst { a , b , c } = getObject(); |
|