TypeScript 等同于 Java 中的 static{ }
TypeScript equivalent to static{ } in Java
我想在访问TS class时自动调用一个方法,类似于java的static{ }
。
例如,如果我在 Java 中实现常量 class:
public class Constants
{
public static final String FOO;
static
{
// ... creates a http request ...
FOO = // ... gets "http:/.../api/foo" from server
}
}
如何在 TypeScript 中实现这样的功能?
目前,我正在使用:
export class Constants
{
public static FOO;
public static ensureInitialized(callback)
{
if (this.FOO == null)
{
let request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
FOO = request.responseText;
callback();
}
};
request.open('POST', "http:/.../api/foo", true);
request.send('');
}
else callback();
}
}
但这需要主动调用 ensureInitialized()
以使用此常量的所有方法。 Java中是否有等同于static{ }
的TS?
你真的应该尝试学习 TypeScript 语言并编写地道的代码。许多程序员在错误的印象下开始使用 TypeScript,认为 TypeScript 添加静态类型会将 JavaScript 变成 Java,但事实并非如此。为了有效地使用 TypeScript,您必须了解 JavaScript。
可以通过学习 TypeScript 很好地学习 JavaScript,但前提是了解 TypeScript 不会 添加任何以下之一:
- class典型的面向对象编程模式
- 标称打字
- 运行时类型信息
在惯用的 TypeScript/JavaScript 中,这种方法没有任何意义,因为可以简洁优雅地实现相同的行为,而 class 上的静态初始化程序的想法是不连贯的。
这是修改后的版本
let foo: string | undefined;
export const Constants = {
get FOO() {
if (!foo) {
fetch("http:/.../api/foo", { method: "POST" })
.then(response => foo = response.text())
.catch(console.error);
}
return foo;
}
}
优点包括简洁、封装以及让人们通过识别成语来理解您在做什么。
注意:存在异步惰性初始化问题,此答案不尝试解决,因为它超出范围。
通常我们期望在使用该值之前预先执行静态
export class Constants {
static foo: any;
static __static_initialized: boolean = false;
static __static_initialize() {
if (Constants.__static_initialized) { return; }
Constants.__static_initialized = true;
fetch("http:/.../api/foo", { method: "POST" })
.then(response => Constants.foo = response.text())
.catch(console.error);
}
}
Constants.__static_initialize();
我想在访问TS class时自动调用一个方法,类似于java的static{ }
。
例如,如果我在 Java 中实现常量 class:
public class Constants
{
public static final String FOO;
static
{
// ... creates a http request ...
FOO = // ... gets "http:/.../api/foo" from server
}
}
如何在 TypeScript 中实现这样的功能?
目前,我正在使用:
export class Constants
{
public static FOO;
public static ensureInitialized(callback)
{
if (this.FOO == null)
{
let request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
FOO = request.responseText;
callback();
}
};
request.open('POST', "http:/.../api/foo", true);
request.send('');
}
else callback();
}
}
但这需要主动调用 ensureInitialized()
以使用此常量的所有方法。 Java中是否有等同于static{ }
的TS?
你真的应该尝试学习 TypeScript 语言并编写地道的代码。许多程序员在错误的印象下开始使用 TypeScript,认为 TypeScript 添加静态类型会将 JavaScript 变成 Java,但事实并非如此。为了有效地使用 TypeScript,您必须了解 JavaScript。
可以通过学习 TypeScript 很好地学习 JavaScript,但前提是了解 TypeScript 不会 添加任何以下之一:
- class典型的面向对象编程模式
- 标称打字
- 运行时类型信息
在惯用的 TypeScript/JavaScript 中,这种方法没有任何意义,因为可以简洁优雅地实现相同的行为,而 class 上的静态初始化程序的想法是不连贯的。
这是修改后的版本
let foo: string | undefined;
export const Constants = {
get FOO() {
if (!foo) {
fetch("http:/.../api/foo", { method: "POST" })
.then(response => foo = response.text())
.catch(console.error);
}
return foo;
}
}
优点包括简洁、封装以及让人们通过识别成语来理解您在做什么。
注意:存在异步惰性初始化问题,此答案不尝试解决,因为它超出范围。
通常我们期望在使用该值之前预先执行静态
export class Constants {
static foo: any;
static __static_initialized: boolean = false;
static __static_initialize() {
if (Constants.__static_initialized) { return; }
Constants.__static_initialized = true;
fetch("http:/.../api/foo", { method: "POST" })
.then(response => Constants.foo = response.text())
.catch(console.error);
}
}
Constants.__static_initialize();