Javascript diamond inheritance structure -
using node looking way around diamond inheritance in javascript:
var util = require('util'); function base(example_value) { console.log(example_value); this.example_property = example_value; this.example_method = function() { ... }; } function foo(settings) { var f = settings.f; base.call(x); this.settings = settings; } util.inherits(foo, base); function bar(settings) { var b = settings.b; base.call(b); this.settings = settings; } util.inherits(bar, base); var foo = new foo({f: 'bar'}); // 'bar' gets printed , can call methods base.. foo.example_method(); var bar = new bar({b: 'foo'}); // 'foo' gets printed , can call methods base.. bar.example_method();
no problems here..but need make available in foo , bar (and base) in encompassing object:
function top(settings) { foo.call(this, settings); bar.call(this, settings); } util.inherits(top, foo); util.inhertis(top, bar); var top = new top({some: 'value'});
'value' gets printed twice isn't after. doing inheritance isn't best way looking alternatives / suggestions deal diamond shape structure.
p.s. haven't included original code modified simplify - i've done hand, don't think there mistakes point i'm trying across should there.
can use delegation?
function top(settings) { this.foo = new foo(settings); this.bar = new bar(settings); } top.prototype.conflictingmethod = function() { // use either this.foo or this.bar } top.prototype.anothermethod = function() { return this.foo.anothermethod(); }
you use mixins, need add class system. ext-js supports mixins http://www.sencha.com/learn/sencha-class-system
// my/sample/cansing.js ext.define('my.sample.cansing', { sing: function(songname) { alert("i'm singing " + songname); } }); // my/sample/canplayguitar.js ext.define('my.sample.canplayguitar', { playguitar: function() { alert("i'm playing guitar"); } }); // my/sample/cancomposesongs.js ext.define('my.sample.cancomposesongs', { composesongs: function() { alert("i'm composing songs"); return this; } }); // my/sample/coolguy.js ext.define('my.sample.coolguy', { extend: 'my.sample.person', mixins: { cansing: 'my.sample.cansing', canplayguitar: 'my.sample.canplayguitar' } }); // my/sample/musician.js ext.define('my.sample.musician', { extend: 'my.sample.person', mixins: { cansing: 'my.sample.cansing', canplayguitar: 'my.sample.canplayguitar', cancomposesongs: 'my.sample.cancomposesongs' } }); // app.js var nicolas = new my.sample.coolguy("nicolas"); nicolas.sing("november rain"); // alerts "i'm singing november rain"
Comments
Post a Comment