NamespaceTest

by philcatterall

HTML

<button onclick="ns1.Hello('From ns1.Hello')">
Click ns1
</button>
<button onclick="ns2.Hello('From ns2.Hello')">
Click ns2
</button>

<div id="div">
start
</div>

JavaScript

var ns1 = ns1 || {};
(function() {
  this.Hello = function(val) {
    document.getElementById('div').innerHTML = val;
  }
}).call(ns1);

const ns2 = {}; //Totall Different Namespace
(function() {
  this.Hello = (val) => {
    document.getElementById('div').innerHTML = val;
  }
}).call(ns2);


var ns1 = ns1 || {};

(function(o) {
  o.Hello = function(val) {
    document.getElementById('div').innerHTML = 'HardCoded';
  }
  o.Hello2 = function(val) {
    document.getElementById('div').innerHTML = val;
  }
})(ns1);
console.log(ns1);