hello world hello world - 4 months ago 13
Javascript Question

Javascript - can class paths have namespaces?

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.

JDB JDB
Answer

Almost there.

var Model = Model || {};
Model.Polygon = class {
    constructor(height, width) {
      this.height = height || 0;
      this.width = width || 0;
    }
}

var myNewPolygon = new Model.Polygon(10, 50);

Classes can be unnamed (aka "anonymous") just like a function, and just like a function, unnamed classes can be assigned to variables, as above with Model.Polygon = class { ... }

If you need the class to reference itself within the body of the class, then you can give it a name. Note that, unlike functions, the class name will not be available outside the body of the class.

var Model = Model || {};
Model.Polygon = class Polygon {
    constructor(height, width) {
      this.height = height || 0;
      this.width = width || 0;
    }

    equals(other){
      // Returns true if other is also an instance of Polygon
      // and height and width are the same.
      return ( other instanceof Polygon )     &&
             ( other.height === this.height ) &&
             ( other.width === this.width );
    }
}

var myNewPolygon1 = new Model.Polygon(10, 50);
var myNewPolygon2 = new Model.Polygon(10, 50);
myNewPolygon1.equals( myNewPolygon2 ); // returns true
myNewPolygon1.equals({ height: 10, width: 50 }); // returns false

var myNewPolygon3 = new Polygon(10, 50); // ERROR! Polygon name isn't 
                                         // recognized outside the Polygon class.
Comments