JSFiddle - React, Tailwind, and code Playground
by ifandelse
JavaScript
var ctor = function (fn) {
var func = function() {
function F(args) {
return fn.apply(this, args);
}
F.prototype = fn.prototype;
return function() {
var args = [].slice.call(arguments);
return new F(args);
}
};
func.prototype = fn.prototype;
func.prototype.constructor = fn;
return func;
};
var Car = function (color) {
if(!color) { throw Error("Color is required!") }
this.color = color;
};
Car.prototype.logColor = function () {
console.log( this.color );
};
Car = ctor(Car);
var a = new Car('blue');
var b = Car('red');
console.log(a.color);
console.log(b.color);
a.logColor();
b.logColor();
console.log( a instanceof Car );
console.log( b instanceof Car );
console.log( a.constructor === b.constructor );