SO Answer / Javascript: Instance name in a class method

See: http://stackoverflow.com/questions/21351015/javascript-instance-name-in-a-class-method

by KooiInc MeHere

HTML

<div id="output_1"></div>
<div id="output_2"></div>
<div class="divider"></div>
<div id="enum"></div>

CSS

[id^=output_] {
    float:left;
    border:1px solid black;
    margin:20px;
}

.divider {
    float:none;
    clear:both;
    border-bottom: 1px dotted #888;
    margin-bottom: 1em;
}

JavaScript

// see also http://jsfiddle.net/KooiInc/4cjg9/embedded/result,js,html,css/
compat();
var JExgTrend = construct();

JExgTrend.create("output_1","testObj1");
testObj1.start("LINK_");
JExgTrend.create("output_2","testObj2");
testObj2.start("LINK_");
document.querySelector('#enum').innerHTML = 'JExgTrend instances are<br>'+
testObj1.enum('<br>');

function construct(){
 return {
  create: function create (divName, name, scope) {
    scope = scope || window;
    var mc = Object.create(this.prototype);
    mc.divName = divName;
    scope[name] = mc.setInstance(name, mc);
    return scope[name];
  },
  prototype: (function(){
    var instances = {};
    return {
      start: function (text) {
		var instanceName = this.getName();
		for (var i=0;i<10;i++) {
			document.querySelector('#'+this.divName)
              .innerHTML += '<a href="#"' + 'data-objId="'+instanceName+'">' + text + i+ '_'+
                            instanceName+'</a><br>';
		}
        var links = document.querySelectorAll('[data-objId]');
        i = 0;
        for (;i<links.length;i+=1) {
            links[i].onclick = function(e){
              window[this.getAttribute('data-objId')].notify();
            };
        }
	  },
      notify: function(msg){
		alert('they call me '+this.getName());
	  },
      setInstance: function (key, val) {
          instances[key] = val;
          return instances[key];
      },
      getName: function () {
          for (var l in instances) {
              if (this === instances[l])
                  return l;
          }
      },
      enum: function (delim) {
        delim = delim || '\n';
        var ret = [];
        for (var l in instances) {
            ret.push(instances[l].getName() + ' => divName: '+instances[l].divName);
        }
        return ret.join(delim);
      }
    }  
  }())
 };
}

function compat(){
 Object.create = Object.create || (function () {
    function F(){}
    
    return function (o) {
      if (arguments.length != 1) {
         ...