Bu arada bu bir dekoratörle birlikte çözülebilir Object.freezeveya Object.definePropertybunu kullanıyorum, tonlarca alıcı kullanmaktan biraz daha güzel. Eylem halinde görmek için bunu doğrudan TS Playground kopyalayabilir / yapıştırabilirsiniz . - İki seçenek var
Alanları tek tek "sonlandır"
Aşağıdaki dekoratör hem açıklamalı statik hem de statik olmayan alanları "salt alıcı özellikleri" ne dönüştürür.
Not : Başlangıç değeri olmayan bir örnek değişkenine açıklama eklenirse @final, ilk atanan değer (ne olursa olsun) son değer olur.
// example
class MyClass {
@final
public finalProp: string = "You shall not change me!";
@final
public static FINAL_FIELD: number = 75;
public static NON_FINAL: string = "I am not final."
}
var myInstance: MyClass = new MyClass();
myInstance.finalProp = "Was I changed?";
MyClass.FINAL_FIELD = 123;
MyClass.NON_FINAL = "I was changed.";
console.log(myInstance.finalProp); // => You shall not change me!
console.log(MyClass.FINAL_FIELD); // => 75
console.log(MyClass.NON_FINAL); // => I was changed.
Dekoratör: Bunu kodunuza eklediğinizden emin olun!
/**
* Turns static and non-static fields into getter-only, and therefor renders them "final".
* To use simply annotate the static or non-static field with: @final
*/
function final(target: any, propertyKey: string) {
const value: any = target[propertyKey];
// if it currently has no value, then wait for the first setter-call
// usually the case with non-static fields
if (!value) {
Object.defineProperty(target, propertyKey, {
set: function (value: any) {
Object.defineProperty(this, propertyKey, {
get: function () {
return value;
},
enumerable: true,
configurable: false
});
},
enumerable: true,
configurable: true
});
} else { // else, set it immediatly
Object.defineProperty(target, propertyKey, {
get: function () {
return value;
},
enumerable: true
});
}
}
Yukarıdaki dekoratöre bir alternatif olarak, bunun katı bir versiyonu da olacaktır, bu da birisi "use strict";ayarlanmakta olan alana bir değer atamaya çalıştığında bir Hata bile atacaktır . (Bu sadece statik kısım)
/**
* Turns static fields into getter-only, and therefor renders them "final".
* Also throws an error in strict mode if the value is tried to be touched.
* To use simply annotate the static field with: @strictFinal
*/
function strictFinal(target: any, propertyKey: string) {
Object.defineProperty(target, propertyKey, {
value: target[propertyKey],
writable: false,
enumerable: true
});
}
Her statik alanı "sonlandır"
Olası Dezavantaj: Bu yalnızca o sınıfın TÜM statik değerleri için veya hiçbiri için kullanılamaz, ancak belirli statik değerlere uygulanamaz.
/**
* Freezes the annotated class, making every static 'final'.
* Usage:
* @StaticsFinal
* class MyClass {
* public static SOME_STATIC: string = "SOME_STATIC";
* //...
* }
*/
function StaticsFinal(target: any) {
Object.freeze(target);
}
// Usage here
@StaticsFinal
class FreezeMe {
public static FROZEN_STATIC: string = "I am frozen";
}
class EditMyStuff {
public static NON_FROZEN_STATIC: string = "I am frozen";
}
// Test here
FreezeMe.FROZEN_STATIC = "I am not frozen.";
EditMyStuff.NON_FROZEN_STATIC = "I am not frozen.";
console.log(FreezeMe.FROZEN_STATIC); // => "I am frozen."
console.log(EditMyStuff.NON_FROZEN_STATIC); // => "I am not frozen."