Hallo,
Anmerkung:
Der Block var SpecialIdFunctor […] kann auch aus dem Block var Foo […] ausgelagert und diesem vorangestellt werden. Die Funktionalität von Foo scheint unverändert, SpecialIdFunctor ist dann jedoch global. Ein Beispiel:
var ExternalIdMixIn = function (that) {
var getExternalId = function () {
return that.getId() + ' external';
};
this.showExternalId = function() {
return getExternalId();
};
};
var Foo = (function () {
var InternalIdMixIn = function (that) {
var getInternalId = function () {
return that.getId() + ' internal';
};
this.showInternalId = function() {
return getInternalId();
};
};
return function (id) {
InternalIdMixIn.call(this, this);
ExternalIdMixIn.call(this, this);
this.getId = function () {
return id;
};
};
return FooConstructor;
})();
var myFoo = new Foo(23);
print(myFoo.getId()); // 23
print(myFoo.showExternalId()); // 23 internal
print(myFoo.showInternalId()); // 23 external
In diesem Falle hätte SpecialIdFunctor eher die Funktion eines MixIns. Lustigerweise hatte ich mit dieser Art Mixin per Function.call() vor zwei, drei Monaten schon experimentiert, aber die Zusammenhänge zu Deinem Beitrag erst gerade eben gesehen.
Gruß
Olaf