Constructorless namespaces in ES5 & ES6 | 2.1 ed.
Tags: javascript, oop
Published: 2022-01-10 19:36:00

### <u>Intro</u>

If you remove constructor from function such function becomes "sterilized" i.e. it becomes object literal subsequently aliased as namespace , although definition of **"namespace"** is not baked-in JavaScript (ECMAScript) specification natively . As ESnext evolving rapidly, well since last ES5 to ES6 for major update around 6 years ago, that time function definition syntactically was switched into definition of **class** , although this is just the way to mock real life class-based programming langs like JAVA . Don't get me wrong , I am not going into specificity , just one two show to common scenarios how to "sterilize" ES5 function / ES6 function (class _syntactically_) making it into nothing else than namespace...

### <u>ES5 constructorless function : </u>

```javascript
const _Static = (function () {
  /* blank : (#) _Static is not a constructor if new'ed */
}.prototype.constructor = {
  /* blank is overwritten with some mocked logic e.g. : */ _this: "local_scope",
  _getter: function () {
    return `Hello from STATIC ${this._this}`;
  }
});
_Static._getter() // 'Hello from STATIC local_scope'
/* new _Static */; // (#)
```

---

### <u>ES6 constructorless function (class)</u>

```javascript
class _Static {
  constructor() {
    return new {}(); // (#) : {} is not a constructor if new'ed
  }

  static _constructor() {
    let _this = {};
    _this.namespace = _Static.name;
    _this.otherProps = "xyz";
    return _this;
  }

  // other static methods
}

_Static._constructor(); // {namespace: '_Static', otherProps: 'xyz'}

/* new _Static(); */ // (#)
```

### <u>BONUS : direct way of making namespace: </u>

You probably noticed , if you used some 2D libs like Konva.js , which uses the following signature whilst initializing things i.e. as `_GlobalNamespace.Method() ` e.g.:

```javascript
const _Konva = {
  Stage: function(){
    // Stage configs...
  }
}

// new instance of Stage accessed within signature of _GlobalNamespace.Method() :
new _Konva.Stage(/* stage configs you pass as documented */);
```
---

### <u>BONUS for 2 ed.</u>

```javascript
// Why this way i.e. first parameter as null :
Math.max.call(null/*, comma seperated params (if any) */)
// But not that way :
Math.max.call(this/*, comma seperated params (if any) */)
// Because Math is not a function constructor, you can double check with typeof Math which would give an 'object', rather than 'function' , consequently no construction, no keyword THIS exploited (upper case just for emphasis) 
```

---

### <u>Related articles</u>

1. [Prototypless namespaces in JavaScript](https://dev.to/projektorius96/prototypeless-namespaces-examples-in-js-2mf9)
2. [Static vs. Singleton in JavaScript](https://dev.to/projektorius96/static-vs-singleton-in-javascript-4ki9)
3. [Visibility modifiers, keyword of static – all in one of JavaScript ES5 standard you need for today](https://dev.to/projektorius96/visibility-modifiers-keyword-of-static-all-in-one-of-javascript-es5-standard-you-need-for-today-eg8)

---

Have anything to add , please leave a comment in the comment section below . See you in the next one !