Para uso adecuado herencia basada Javascript-prototipo que podría utilizar fastClass
https://github.com/dotnetwise/Javascript-FastClass
Usted tiene el más simple inheritWith
sabor:
var Mammal = function (spec) {
this.spec = spec;
}.define({
clearThroat: function() { return "Ahem" },
getName: function() {
return this.spec.name;
},
says: function() {
return this.clearThroat() + ' ' + spec.saying || '';
}
});
var Cat = Mammal.inheritWith(function (base, baseCtor) {
return {
constructor: function(spec) {
spec = spec || {};
baseCtor.call(this, spec);
},
purr: function() {
return this.clearThroat() + " purr";
},
getName: function() {
return this.says() + ' ' + this.spec.name + this.says();
}
}
});
var kitty = new Cat({ name: "Fluffy" });
kitty.purr(); // Ahem purr
kitty.getName(); // Ahem Fluffy Ahem
Y si se está muy preocupado por el rendimiento entonces usted tiene el sabor fastClass
:
var Mammal = function (spec) {
this.spec = spec;
}.define({
clearThroat: function() { return "Ahem" },
getName: function() {
return this.spec.name;
},
says: function() {
return this.clearThroat() + ' ' + spec.saying || '';
}
});
var Cat = Mammal.fastClass(function (base, baseCtor) {
return function() {
this.constructor = function(spec) {
spec = spec || {};
baseCtor.call(this, spec);
};
this.purr = function() {
return this.clearThroat() + " purr";
},
this.getName = function() {
return this.says() + ' ' + this.spec.name + this.says();
}
}
});
var kitty = new Cat({ name: "Fluffy" });
kitty.purr(); // Ahem purr
kitty.getName(); // Ahem Fluffy Ahem
Por cierto, su código inicial no hace ningún sentido pero lo he respetado literalmente.
fastClass
utilidad:
Function.prototype.fastClass = function (creator) {
var baseClass = this, ctor = (creator || function() { this.constructor = function() { baseClass.apply(this, arguments); } })(this.prototype, this)
var derrivedProrotype = new ctor();
if (!derrivedProrotype.hasOwnProperty("constructor"))
derrivedProrotype.constructor = function() { baseClass.apply(this, arguments); }
derrivedProrotype.constructor.prototype = derrivedProrotype;
return derrivedProrotype.constructor;
};
inheritWith
utilidad:
Function.prototype.inheritWith = function (creator, makeConstructorNotEnumerable) {
var baseCtor = this;
var creatorResult = creator.call(this, this.prototype, this) || {};
var Derrived = creatorResult.constructor ||
function defaultCtor() {
baseCtor.apply(this, arguments);
};
var derrivedPrototype;
function __() { };
__.prototype = this.prototype;
Derrived.prototype = derrivedPrototype = new __;
for (var p in creatorResult)
derrivedPrototype[p] = creatorResult[p];
if (makeConstructorNotEnumerable && canDefineNonEnumerableProperty) //this is not default as it carries over some performance overhead
Object.defineProperty(derrivedPrototype, 'constructor', {
enumerable: false,
value: Derrived
});
return Derrived;
};
define
utilidad:
Function.prototype.define = function (prototype) {
var extendeePrototype = this.prototype;
if (prototype)
for (var p in prototype)
extendeePrototype[p] = prototype[p];
return this;
}
[* Nota, yo soy el autor del paquete de código abierto y los nombres de los mismos métodos podrían ser renombrados en future` *]
"* tiene que volver a compilar todas las funciones en él. Es decir, no puede compartir el código entre instancias * "- No. El código es compartido, solo se deben crear diferentes objetos de función con diferentes valores de ámbito. No es una gran sobrecarga. – Bergi