JSFiddle - React, Tailwind, and code Playground

by Joshua

JavaScript

this.tree = function(f){
	this.roots = roots = function(_super, _trunk, f, _self){
		_self = _self || this; _trunk = _trunk || this; _super = _super || this;
		[
			['branch', function(f, args){
				args = args || [];
				var r = new roots(_self, _trunk, f);
				f.apply(r, args); return r;
			}],
			['leaf', function(f, r){
				return f.bind(new roots(_self, _trunk, f));
			}],
			['compile', function(){
				return new _self.branch(_self.user_function, arguments);
			}],
			['user_function', f],
			['trunk', _trunk],
			['super', _super]
		].forEach(function(pair){
			Object.defineProperty(_self, pair[0], { enumerable: false, value: pair[1] });
		});
		return _self;
	};
	return function(){
		var inst = new roots();
		f.apply(inst, arguments);
		return inst;
	}
}

var a = new tree(function(a){
	this.val = a;
	this.a = new this.branch(function(d){
		this.d = d;
		this.b = {
			c: new this.leaf(function(d){
				return this;
			})
		}
		return this;
	}, [5]);
});

var inst = new a(2);
inst.val = 3;
console.log(inst.a.trunk);
console.log(inst.a.compile(5).d);