var log = Helpers.log2Screen;
function Foo(name) { this.name = name || 'just Foo' };
Foo.prototype.log = log;
function Bar() {
// if assigned within the constructor,
// you should at least check if
// that is already done.
// *Foo prototype -> Bar will now
// be assigned on *every* instantiation
// (see logging in result)
Bar.prototype = Foo.prototype;
Bar.prototype.log('*Foo prototype -> Bar');
};
// copy methods
function FooBar() {
if (!FooBar.prototype.log) {
for (var l in Foo.prototype) {
FooBar.prototype[l] = Foo.prototype[l];
}
FooBar.prototype.log('*Foo prototype <i>copied</i> to FooBar');
}
}
// assign prototype from Foo
function BarFoo() {
if (!BarFoo.prototype.log) {
BarFoo.prototype = Object.create(Foo.prototype);
BarFoo.prototype.logExtended = function (msg) {
this.log('<b>extra!</b> '+msg);
};
BarFoo.prototype.log('*Foo prototype -> BarFoo');
}
}
// this will succeed
function FooFoo() {
if (!FooFoo.prototype.log) {
FooFoo.prototype = Object.create(Foo.prototype);
FooFoo.prototype.log('*Foo prototype -> FooFoo');
return new FooFoo;
}
}
// outside creation
function BarBar() { };
BarBar.prototype = Object.create(Foo.prototype);
var bar = new Bar; // will log *Foo prototype -> Bar
var bar2 = new Bar; // will again log *Foo prototype -> Bar
var foobar = new FooBar;
var barfoo1 = new BarFoo;
var barfoo2 = new BarFoo;
var doublefoo = new FooFoo;
var doublebar = new BarBar;
try { bar.log('hello!'); }
catch(e) { log('`bar.log` failed => ', e.message); }
try { barfoo1.log('`barfoo1` (first instance BarFoo) hello!'); }
catch(e) { log('`barfoo1.log` (first instance BarFoo) failed => ', e.message); }
try { barfoo2.log('`barfoo2.log` (second instance BarFoo) succeeds: <b>hello!</b>'); }
catch(e) { log('`barfoo2.log` (second instance BarFoo) failed => ', e.message);...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.