0

I´m trying to implement some nested namespaces with objects, and objects that inherit from those objects. I want to know if this first approach is well implemented:

// Namespaces
var firstDomain = {};
firstDomain.firstSubDomain = {};
firstDomain.firstSubDomain.secondSubDomain = {}

/*
* Base Object
*/
firstDomain.firstSubDomain.secondSubDomain.baseObject = (function(baseObject) {
  baseObject.init = function() {};

  return {
    init: baseObject.init
  }

})(firstDomain.firstSubDomain.secondSubDomain.baseObject || (firstDomain.firstSubDomain.secondSubDomain.baseObject = {}));

/*
* Extended Object
*/
firstDomain.firstSubDomain.secondSubDomain.extendedObject = (function(extendedObject) {
    extendedObject.init = function() {};
    extendedObject.init.prototype = Object.create(firstDomain.firstSubDomain.secondSubDomain.baseObject.init.prototype);
    extendedObject.init.prototype.extraFunction = function() {};

    return {
      init: extendedObject.init
    }

})(firstDomain.firstSubDomain.secondSubDomain.extendedObject || (firstDomain.firstSubDomain.secondSubDomain.extendedObject = {}))

// Objects instances

// Base object instance
var baseObject = new firstDomain.firstSubDomain.secondSubDomain.baseObject.init();

// Extended object instance and executing particular method
var extendedObject = new firstDomain.firstSubDomain.secondSubDomain.extendedObject.init();
extendedObject.extraFunction();

console.log(firstDomain);

Here is the repo link:

https://github.com/termosfera/JS_Design_Patterns/blob/master/namespace_subclassing/nested_namespaced_subclassing.js

juanmorschrott
  • 573
  • 5
  • 25
  • 1
    *well implemented* in what sense? – Liam Jan 25 '16 at 16:52
  • Hi @Liam, thanks for your answer. I´m trying to find the best way to implement inheritance to namespaced objects. Do you know any link or tutorial where I could find something about this? I googled it but I didn´t find this implementation. The point is get this: new firstDomain.firstSubDomain.SecondSubdomain.baseObject() – juanmorschrott Jan 25 '16 at 17:18
  • 1
    [Try this](https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) – Liam Jan 25 '16 at 17:25
  • Thanks, I finally find a better approach: https://github.com/termosfera/JS_Design_Patterns/blob/master/namespace_subclassing/nested_namespaced_subclassing.js – juanmorschrott Jan 25 '16 at 17:33
  • You really should've named it `firstSubSubDomain`. "second" is very confusing here. – Bergi Jan 25 '16 at 17:39
  • You'll want to have a look at https://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern/9321429#9321429 – Bergi Jan 25 '16 at 17:40

1 Answers1

2

That reads like two questions in one, as namespacing and subclassing are two distinct things.

…baseObject = (function(baseObject) {
  baseObject.… = …

  return {
    …: baseObject.…
  }

})(…baseObject || (…baseObject = {}));

No, that's not correct. You're mixing the revealing module pattern with the decorating module1 pattern here. You should be doing either

….baseObject = (function() {
  return {
    …: …
  };
})();

or

(function(baseObject) {
  baseObject.… = …;
})(….baseObject || (….baseObject = {}));

1: I actually have no idea what the second pattern is called

… = function() {};
….prototype = Object.create(other….prototype);
….prototype.extraFunction = function() {};

That's fine, your inheritance from the prototype is correct. However, you might want to do a super call in constructor.

new ….baseObject.init();
new ….extendedObject.init();

That's weird though. There's no reason to name a constructor "init". Also constructors should be capitalised. It should be like

new ….BaseObject();
new ….ExtendedObject();
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks @Bergi yeah I finally made this implementation (basically what you say) https://github.com/termosfera/JS_Design_Patterns/blob/master/namespace_subclassing/nested_namespaced_subclassing.js – juanmorschrott Jan 25 '16 at 18:39
  • 1
    That `my` object is still superfluous. You should just use a function declaration `function BaseObject() {…}` and then `return BaseObject;` – Bergi Jan 25 '16 at 19:03