JSFiddle - React, Tailwind, and code Playground
by blesh
JavaScript
//Dynamic prototyping
//create a class
function Foo() {
}
//create a child class
function Child() {
}
//now give Foo a method for creating children of itself.
Foo.prototype.newChild = function (){
//dynamically set the prototype
Child.prototype = this;
//return a new child!
return new Child();
};
//try it out!
var foo = new Foo();
foo.test = '123';
var bar = foo.newChild();
printLn(bar.test); //123
foo.test += '456';
printLn(bar.test) //123456
foo.blah = 'wee';
printLn(bar.blah); //wee
function printLn(x) {
document.write('<p>' + x + '</p>');
}