如何将旧 js 库的闭包转换为 typescript class?
How to turn closure from old js library into typescript class?
您好,我正在尝试将 JS 库转换为 TypeScript。
Link 到图书馆:https://github.com/msarhan/hijrah-date
我发现很难将 main 函数转换为 class。我不明白为什么 HijrahDate
有一个 HijrahDate
函数?这是创建构造函数的旧方法吗?
我想要实现的是将(如果可能的话)旧的 JS 代码转换为 Typescript class.
简化版
var HijrahDate = (function(){
HijrahDate.isValid = isValid;
HijrahDate.isHijrahDate = isHijrahDate;
HijrahDate.prototype.getTime = getTime;
HijrahDate.prototype.setFullYear = setFullYear;
…
init();
function HijrahDate(year, monthOfYear, dayOfMonth, hours, minutes, seconds, milliseconds){
this._year = Number.NaN;
this._monthOfYear = Number.NaN;
this._dayOfMonth = Number.NaN;
var y, m, d, g;
…
}
…
function init(){
var years = [];
years['1300']=[30,29,30,29,30,29,30,29,30,29,30,29];
… //rest of years till years['1600']
var minYear = 1300;
var maxYear = 1600;
var isoStart = epochDayFromGregorain(new Date(1882, 11-1, 12));
hijrahStartEpochMonth = minYear * 12;
minEpochDay = isoStart;
hijrahEpochMonthStartDays = createEpochMonths(minEpochDay, minYear, maxYear, years);
maxEpochDay = hijrahEpochMonthStartDays[hijrahEpochMonthStartDays.length - 1];
minYearLength = 0, maxYearLength = 0;
for (var year = minYear; year < maxYear; year++) {
var length = _getYearLength(year);
minYearLength = Math.min(minYearLength, length);
maxYearLength = Math.max(maxYearLength, length);
}
…
}
return HijrahDate;
})();
...
到目前为止,我已经尝试创建一个 class 构造函数,其中包含 init
export class HijrahDate {
year: number;
monthOfYear: number;
dayOfMonth: number;
hours?: number;
minutes?: number;
private _gregorianDate: any;
private _monthOfYear: any;
private _dayOfMonth: any;
seconds?: number;
milliseconds?: number;
constructor(
year: number,
monthOfYear: number,
dayOfMonth: number,
hours?: number,
minutes?: number,
seconds?: number,
milliseconds?: number
) {
this.year = year;
this.monthOfYear = monthOfYear;
this.dayOfMonth = dayOfMonth;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.milliseconds = milliseconds;
this._gregorianDate = new Date();
this.init();
}
javascript 实现通过 IIFE 生成 class,这有效地提供了一种无需事先本机支持即可生成 'private' API 的模式。 init
函数本身可以被认为是私有 API 的一部分。调用时,它实际上从未在返回的 class 上公开。此外,init
依赖于某些您尚未声明的私有变量,例如 maxEpochDay
。
任何最终未通过 IIFE 在返回的 class 上公开的函数或变量都可以使用 ts 声明为私有。那些在 IIFE 中的原型 属性 上定义的可以被添加为 class 方法,默认情况下是 public 或者可以标记为清楚。
您好,我正在尝试将 JS 库转换为 TypeScript。
Link 到图书馆:https://github.com/msarhan/hijrah-date
我发现很难将 main 函数转换为 class。我不明白为什么 HijrahDate
有一个 HijrahDate
函数?这是创建构造函数的旧方法吗?
我想要实现的是将(如果可能的话)旧的 JS 代码转换为 Typescript class.
简化版
var HijrahDate = (function(){
HijrahDate.isValid = isValid;
HijrahDate.isHijrahDate = isHijrahDate;
HijrahDate.prototype.getTime = getTime;
HijrahDate.prototype.setFullYear = setFullYear;
…
init();
function HijrahDate(year, monthOfYear, dayOfMonth, hours, minutes, seconds, milliseconds){
this._year = Number.NaN;
this._monthOfYear = Number.NaN;
this._dayOfMonth = Number.NaN;
var y, m, d, g;
…
}
…
function init(){
var years = [];
years['1300']=[30,29,30,29,30,29,30,29,30,29,30,29];
… //rest of years till years['1600']
var minYear = 1300;
var maxYear = 1600;
var isoStart = epochDayFromGregorain(new Date(1882, 11-1, 12));
hijrahStartEpochMonth = minYear * 12;
minEpochDay = isoStart;
hijrahEpochMonthStartDays = createEpochMonths(minEpochDay, minYear, maxYear, years);
maxEpochDay = hijrahEpochMonthStartDays[hijrahEpochMonthStartDays.length - 1];
minYearLength = 0, maxYearLength = 0;
for (var year = minYear; year < maxYear; year++) {
var length = _getYearLength(year);
minYearLength = Math.min(minYearLength, length);
maxYearLength = Math.max(maxYearLength, length);
}
…
}
return HijrahDate;
})();
...
到目前为止,我已经尝试创建一个 class 构造函数,其中包含 init
export class HijrahDate {
year: number;
monthOfYear: number;
dayOfMonth: number;
hours?: number;
minutes?: number;
private _gregorianDate: any;
private _monthOfYear: any;
private _dayOfMonth: any;
seconds?: number;
milliseconds?: number;
constructor(
year: number,
monthOfYear: number,
dayOfMonth: number,
hours?: number,
minutes?: number,
seconds?: number,
milliseconds?: number
) {
this.year = year;
this.monthOfYear = monthOfYear;
this.dayOfMonth = dayOfMonth;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.milliseconds = milliseconds;
this._gregorianDate = new Date();
this.init();
}
javascript 实现通过 IIFE 生成 class,这有效地提供了一种无需事先本机支持即可生成 'private' API 的模式。 init
函数本身可以被认为是私有 API 的一部分。调用时,它实际上从未在返回的 class 上公开。此外,init
依赖于某些您尚未声明的私有变量,例如 maxEpochDay
。
任何最终未通过 IIFE 在返回的 class 上公开的函数或变量都可以使用 ts 声明为私有。那些在 IIFE 中的原型 属性 上定义的可以被添加为 class 方法,默认情况下是 public 或者可以标记为清楚。