Challenge: Solution Review

This lesson will explain the solution to the problem from the previous coding challenge.

We'll cover the following...

Solution #

Press + to interact
let configure = null;
class ConfigureVals{
constructor(initvalues){
this.xpoint = initvalues.xpoint || 0;
this.ypoint = initvalues.ypoint || 0;
this.shape = initvalues.shape || null;
}
static getConfiguration(initvalues){
if (!configure) {
configure = new ConfigureVals(initvalues)
}
return configure;
}
}
var configureObj1 = ConfigureVals.getConfiguration({ "xpoint": 8, "ypoint" : 9, "shape" : "rectangle" });
console.log(configureObj1);
var configureObj2 = ConfigureVals.getConfiguration({ "xpoint": 2, "ypoint": 4, "shape" : "circle" });
console.log(configureObj2);

Explanation

In the solution, the first line defines the variable configure which stores the reference to the first instance of ConfigureVals. This variable is only initialized once.

The constructor of the class accepts the object parameter initvalues. It defines and initializes the values xpoint, ypoint, ...