JSFiddle - React, Tailwind, and code Playground
by Gleb
JavaScript
function surrogateCtor() {}
function extend(base, sub, methods) {
surrogateCtor.prototype = base.prototype;
sub.prototype = new surrogateCtor();
sub.prototype.constructor = sub;
// Add a reference to the parent's prototype
sub.base = base.prototype;
// Copy the methods passed in to the prototype
for (var name in methods) {
sub.prototype[name] = methods[name];
}
// so we can define the constructor inline
return sub;
}
var service = function(aaa){
privat = 'parent service';
return {
get : function() { privat+aaa },
set : function(value){
return aaa
}
}
};
//servive.prototype.get = function(){ return aaa + '!' };
var newService = (function(){
// $this refers to the constructor
var bbb;
var $this = function (aaa) {
bbb = aaa;
// Look, no hardcoded reference to this class's name
$this.base.constructor.call(this, aaa);
};
extend(service, $this, {
get: function(){
return bbb + '!';
}
});
return $this;
})();
var ns = new newService('bla bla');
console.log(ns.get()); //should bla bla!
console.log(ns.set()); //should be bla bla