We can use destructuring assignment for objects also.
Syntax:
let {var1, var2} = {var1:…, var2:…}
Right side is an existing object which that we want to split into variables. Left side is our pattern to split variables.
Basic Example I:
Internally its uses like , options.title, options.width and options.height. Hope you got it.
and order does not matter here. Anywhere it will work.
Example II:
Above order was changed, still its working perfectly.
Rest Operator:
Below we have used Rest operator with object. So other than title, remaining will come under rest. See below code.
Syntax:
let {var1, var2} = {var1:…, var2:…}
Right side is an existing object which that we want to split into variables. Left side is our pattern to split variables.
Basic Example I:
let options = { title: "MasterPage", width: 100, height: 200 }; let {title, width, height} = options; alert(title); // MasterPage alert(width); // 100 alert(height); // 200
Internally its uses like , options.title, options.width and options.height. Hope you got it.
and order does not matter here. Anywhere it will work.
Example II:
let options = { title: "MasterPage", height: 200, width: 100 }; let {title, width, height} = options; alert(title); // MasterPage alert(width); // 100 alert(height); // 200
Above order was changed, still its working perfectly.
Rest Operator:
Below we have used Rest operator with object. So other than title, remaining will come under rest. See below code.
let options = { title: "MasterPage", height: 200, width: 100 }; let {title, ...rest} = options; // now title="MasterPage", rest={height: 200, width: 100} alert(rest.height); // 200 alert(rest.width); // 100
No comments:
Post a Comment