JSFiddle - React, Tailwind, and code Playground
by Anatol
CSS
.true { color:green; }
.false { color:red; }
JavaScript
(function() {
'use strict';
// Model
var myapp={nodes:{models:{}}};
myapp.nodes.models={
geometry: {
cube: function() {
test(myapp.constructorOf(this), 'myapp.nodes.models.geometry.cube');
}
},
math: {
cube: function() {
test(myapp.constructorOf(this), 'myapp.nodes.models.math.cube');
}
}
};
myapp.nodes.models.math.cube.prototype.test=function() {
test(myapp.constructorOf(this), 'myapp.nodes.models.math.cube');
};
// Find namespace
myapp.constructorOf=(function(indexFor, name) {
var index;
function buildIndex(obj, path) {
index=index || {};
Object.keys(obj).forEach(function(key) {
var newPath=(path || name)+'.'+key, val=obj[key];
index[val]=newPath;
if (Object.keys(val).length) {
buildIndex(val, newPath);
}
});
}
return function(instance) {
if (!index) {
buildIndex(indexFor);
}
return index[instance.constructor];
};
})(myapp, 'myapp');
// Test
function test(a, b) {
document.write('<div class="'+(a===b)+'">'+a+' === '+b+'</div>');
}
test('this should', 'fail');
(new myapp.nodes.models.math.cube()).test();
new myapp.nodes.models.geometry.cube();
})();