JSFiddle - React, Tailwind, and code Playground
by amatiasq
HTML
<script src="http://amatiasq.com/fiddle-console.js"></script>
JavaScript
function type2ctor(Type) {
function Ctor() {
Type.constructor.apply(this, arguments);
}
Ctor.prototype = Type;
return Ctor;
}
var Vector = {
constructor: function(x, y) {
this.x = x;
this.y = y;
}
};
var VectorCtor = type2ctor(Vector);
var vector = new VectorCtor(2, 3);
console.log(vector.x);
var Person = {
// no constructor
sayHi: function () {
console.log('Hi all!');
},
};
var PersonCtor = type2ctor(Person);
var bob = new PersonCtor();
bob.sayHi();