You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Method #1classFoo{constructor(options){({one: this.one,two: this.two}=options);// Do something else with the other options here}}// Method 2classFoo{constructor(options){const{one, two}=options;Object.assign(this,{one, two});// Do something else with the other options here}}// Method 3// If you want to apply all your options to the instance, you could use Object.assign without destructuring:classFoo{constructor(options){Object.assign(this,options);}}
Destructure Examples
// Object Destructure - defaultconst{ foo, bar }={foo: 'hello',bar: 'world'}console.log(foo)// gibt 'hello' ausconsole.log(bar)// gibt 'world' aus// Object Destructure - with variableconsttest={foo: 'hello',bar: 'world'}const{ foo, bar }=testconsole.log(foo)// gibt 'hello' ausconsole.log(bar)// gibt 'world' aus// Object Destructure - with default valueconst{ foo, bar='myworld'}={foo: 'hello'}console.log(foo)// gibt 'hello' ausconsole.log(bar)// gibt 'myworld' aus// Object Destructure - with default value that gets overwrittenconst{ foo, bar='myworld'}={foo: 'hello',bar: 'mybetterworld'}console.log(foo)// gibt 'hello' ausconsole.log(bar)// gibt 'mybetterworld' aus// Object Destructure - rename key value to new variableletobj={a: true,b: false};let{a: lena}=obj;console.log(lena);// true// Object Destructure - with default value and empty object if no param was passedconst{ foo='hey', bar='myworld'}={}console.log(foo)// gibt 'hey' ausconsole.log(bar)// gibt 'myworld' aus// Object Destructure - with functionfunctionadd({x,y}){returnx+y}constresult=add({x:2,y:3})console.log(result)// gibt 5 aus// Object Destructure - with function and spread operatorletobj1={a: true};letobj2={b: true};functiontest({a =false, b =false}){console.log(a);console.log(b);};test({...obj1, ...obj2});// Object Destructure - with function and empty param z return NaNfunctionadd({x,y,z}){returnx+y+z}constresult=add({x:2,y:3})console.log(result)// gibt NaN aus, weil z nicht definiert wurde// Object Destructure - with function and default paramsfunctionadd({x=0,y=0,z=0}){returnx+y+z}constresult=add({x:2,y:3})console.log(result)// gibt 5 aus// Object Destructure - with function and default params but no params are passedfunctionadd({x=0,y=0,z=0}){returnx+y+z}constresult=add()// wirft direkt einen Fehler "Uncaught TypeError: (destructured parameter) is undefined"// Object Destructure - with function and default params but no params are passed. We use empty object to solve this problemfunctionadd({x=0,y=0,z=0}={}){returnx+y+z}constresult=add()console.log(result)// gibt 0 aus// same like above but more advanced examplefunctiondrawChart({size ='big', coords ={x: 0,y: 0}, radius =25}={}){console.log(size);console.log(coords);console.log(radius);}drawChart({coords: {x: 18,y: 30},radius: 30})// gibt folgende drei Zeilen aus:big{x: 18,y: 30}30