function Person(fname = 'First Name', lname = 'Last Name', age = 'Age') {
this.fname = fname;
this.lname = lname;
this.age = age;
}
let person1 = new Person('husain', undefined, 1998);
console.log('person1: ', person1);
// output: Person {fname: "husain", lname: "Last Name", age: 1998} (as I want)
In this example of a constructor of a function class, while creating an instance, I want to use default values of the parameters defined in the constructor, but without following the order at which the parameters are declared in it.
One way I have found out which is shown in the code (passing undefined
to the arguments which we want to specify (so that it will use the default value) and after that continuing to pass other arguments normally).