I am trying to define a Javascript class with a defined constructor with params with the most proper ES6 syntax. At first, it is easy to define it like this.
let param1 = 10;
let param2 = 'foo';
let param3 = 200;
let param4 = 'bar';
let props = {id: param1, options: { op1: param2, op2: param3, op3: param4 }};
console.log('Object props');
console.log(props);
class Test {
constructor(props){
this.id = props.id;
this.options = props.options;
}
}
let test1 = new Test(props);
console.log('Class test1');
console.log(test1.id);
console.log(test1.options.op2);
But when I try to use destructuring to define default values, for one of the params of the constructor (op2
, a property of the nested object options
),I am not able to make it work, while for the id
property of the object I am able:
let param1 = 10;
let param2 = 'foo';
let param3 = 200;
let param4 = 'bar';
let props = {options: { op1: param2, op3: param4 }};
console.log('Object props');
console.log(props);
class Test {
constructor({id = 'defaultId', options = { op2:'0'}} = {}){
this.id = id;
this.options = options;
}
}
let test = new Test(props);
console.log('Class test');
console.log(test.id);
console.log(test.options);
console.log(test.options.op2);
What I should expect is that when debugging with console.log(test.options.op2)
prints the default value set in the constructor, but instead I am gettting undefined
.
Also I would like to know if is there any more proper ES6 syntax when defining javascript classes to initialize class params.
Function parameters with default values are initialized with default values if they contain no value or are undefined. JavaScript function parameters are defined as undefined by default. However, it may be useful to set a different default value.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
The default constructor in Java initializes the data members of the class to their default values such as 0 for int, 0.0 for double etc. This constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class.
Default parameters allow us to initialize functions with default values. A default is used when an argument is either omitted or undefined — meaning null is a valid value. A default parameter can be anything from a number to another function.
But when I try to use destructuring to define default values
constructor({id = 'defaultId', options = { op2:'0'}} = {}){
for one of the params of the constructor (op2, a property of the nested object options), I am not able to make it work, while for the id property of the object I am able.
Default values only work on the single parameter/property level. If no argument or undefined
is passed, {}
will be used for the first parameter. If the object does not have an id
property, 'defaultId'
will be used. If the object does not have an options
property, {op2:'0'}
will be used. If you do pass an object with an options
property, the default value will be ignored.
So if you want a default value for the op2
property of the object if no such property was found in the object, you need to use destructuring on the options
object itself:
constructor({id = 'defaultId', options: {op1, op2 = '0', op3} = {}} = {}) {
this.id = id;
this.options = {op1, op2, op3};
}
You can not actually add missing properties to an object as it's never added because options object does not fallback to default values. In this particaular scenario it would be much simplier to go this way:
class Test {
constructor({id = 'defaultId', options= { op2:'0'}} = {}){
this.id = id;
this.options = options;
this.options.op2 = this.options.op2 || '0';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With