I know I can create a class with this code:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
However, I would like this Polygon
class to reside within a namespace called Model
so that I can instantiate Polygon
objects like this:
var myNewPolygon = new Model.Polygon(10, 50);
Is this possible?
I have tried the following:
var Model = Model || {};
class Model.Polygon {
constructor() {
this.height = height;
this.width = width;
}
}
var myNewPolygon = new Model.Polygon(10, 50);
But this results in Uncaught SyntaxError: Unexpected token .
on line 2.
I have also tried:
var Model = Model || {};
class Polygon {
constructor(height, width) {
this.height = height || 0;
this.width = width || 0;
}
}
Model.Polygon = new Polygon();
var myNewPolygon = new Model.Polygon(10, 50);
But this results in Uncaught TypeError: Model.Polygon is not a constructor
on line 9.