Typescript 访问同一 class 中的私有静态字段
Typescript access to private static fields within same class
我想知道在同一个 class 中访问私有静态字段的方法是什么,假设 class 不是 exported。
module Test {
class Template {
private static ext = '.hbs';
private static basePath = 'WebContent/templates/';
private static templatesFolder = 'templates';
private static partialsFolder = 'partials';
private static paymentMethodsFolder = 'paymentMethods';
public static template(templateName, data): string{
return Handlebars.templates[Test.Template.basePath + this.templatesFolder + '/' + templateName + this.ext];
}
}
}
我不知道如何访问 static template
函数中的静态变量。我不想导出 class,因为我想封装逻辑,使其无法在浏览器中使用。
到这里怎么走?我从 this
开始,因为它一开始不是静态的,但我改变了主意,现在卡住了。
在这种情况下,您可以像下面这样输入 class 名称(与现在相同,但没有模块名称(测试)
Handlebars.templates[Template.basePath + Template.templatesFolder + '/' + templateName + Template.ext];
您现在无法从模块外部访问 public static template
函数,因为它位于未导出的 class 中。
如果您将该函数从模块中的 class 移出,而不是 public static
使其成为 export function
,您可以在模块外部调用它,它应该可以正常工作。
我想知道在同一个 class 中访问私有静态字段的方法是什么,假设 class 不是 exported。
module Test {
class Template {
private static ext = '.hbs';
private static basePath = 'WebContent/templates/';
private static templatesFolder = 'templates';
private static partialsFolder = 'partials';
private static paymentMethodsFolder = 'paymentMethods';
public static template(templateName, data): string{
return Handlebars.templates[Test.Template.basePath + this.templatesFolder + '/' + templateName + this.ext];
}
}
}
我不知道如何访问 static template
函数中的静态变量。我不想导出 class,因为我想封装逻辑,使其无法在浏览器中使用。
到这里怎么走?我从 this
开始,因为它一开始不是静态的,但我改变了主意,现在卡住了。
在这种情况下,您可以像下面这样输入 class 名称(与现在相同,但没有模块名称(测试)
Handlebars.templates[Template.basePath + Template.templatesFolder + '/' + templateName + Template.ext];
您现在无法从模块外部访问 public static template
函数,因为它位于未导出的 class 中。
如果您将该函数从模块中的 class 移出,而不是 public static
使其成为 export function
,您可以在模块外部调用它,它应该可以正常工作。