kullanabilirdin fastClass
https://github.com/dotnetwise/Javascript-FastClass
Sen basit inheritWith
lezzet:
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
Ve performansı hakkında çok endişe varsa o zaman fastClass
lezzet:
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
Btw, ilk kodunuz ama anlamıyla buna saygı duydum.
fastClass
yarar:
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
yarar:
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
yarar:
Function.prototype.define = function (prototype) {
var extendeePrototype = this.prototype;
if (prototype)
for (var p in prototype)
extendeePrototype[p] = prototype[p];
return this;
}
[* Yasal Uyarı Ben açık kaynak paketi ve isimlerin yazarıyım yöntemlerin kendileri yeniden adlandırılabilir future` *]
"* İçinde yeniden derlemek tüm fonksiyonlar zorundadır. Yani, kodları örnekler arasında paylaşamazsınız * "- Hayır. Kod paylaşıldı, yalnızca farklı kapsam değerlerine sahip farklı işlev nesneleri oluşturulmalı. O kadar büyük bir yük değil. – Bergi