DEV Community

Discussion on: Objective JS with IIFE

Collapse
zoedreams profile image
☮️✝️☪️🕉☸️✡️☯️ • Edited on

check this out. i wrote this a few years back. nice to see someone else come to the same solution. great work!

class Clazz {
  constructor() {
    var _level = 1

    function _private(x) {
      return _level * x;
    }
    return {
      level: _level,
      public: this.private,
      public2: function(x) {
        return _private(x);
      },
      public3: function(x) {
        return _private(x) * this.public(x);
      },
    };
  }

  private(x) {
    return x * x;
  }
}

var clazz = new Clazz();

console.log(clazz._level); //undefined
console.log(clazz._private); // undefined
console.log(clazz.level); // 1
console.log(clazz.public(1)); //1
console.log(clazz.public2(2)); //2
console.log(clazz.public3(3)); //27
console.log(clazz.private(0)); //error

my answer on stackoverflow stackoverflow.com/a/46707381/334927

***please note that not declaring the namespace with class or a symbol reference, the type checking will be reverted to string and shallow. This means that deep typeof or === object compares will most likely will false positives.such as isBool: true !== "true" || true || 0