在不同的 TypeScript 之间共享和改变对象 类
Share and mutate object between different TypeScript classes
我有以下 TypeScript 代码:
class ClassA {
options: ClassOption;
B: ClassB;
constructor() {
this.B = new ClassB(this.options);
this.changeOptions();
}
changeOptions(): void {
const newOptions: ClassOption = new ClassOption("something");
this.options = newOptions;
}
}
class ClassB {
options: ClassOption;
constructor(opts: ClassOptions) {
this.options = opts;
}
someFunction(): ClassOption {
return this.options;
}
}
问题是当我实例化时 ClassA
:
const a = new ClassA();
a.B.someFunction();
returns undefined
而不是从 ClassA
的 changeOptions
方法设置的新选项。
当在 ClassA
的构造函数中时你做:
this.B = new ClassB(this.options);
this.options
仍然是 undefined
,所以基本上当在 ClassB
的构造函数中你做:
this.options = opt;
您只是将 this.options
设置为 undefined
而不是为其分配对 ClassA
的 options
的引用,后者不存在,因为它不存在已初始化。
即使您在 ClassA
中用空对象初始化 options
,如果您为它分配 (this.options = something
) 一个新值,ClassB
也不会引用新值。
你想做的是:
用空对象初始化 ClassA
的 this.options
:
options: ClassOption = {};
将其传递给 ClassB
的构造函数。此处无需更改。
调用 ChangeOptions
时,改变同一个对象而不是用新对象替换它。您可以使用 Object.assign
合并两个对象:
changeOptions(): void {
const newOptions: ClassOption = new ClassOption("something");
Object.assign(this.options, newOptions);
// Note that after this, `this.options`' reference is preserved.
}
在这里你可以看到它以普通的方式工作 JavaScript:
class ClassA {
constructor() {
// 1. Initialise with an empty object:
this.options = {};
// 2. Pass that reference to ClassB instead of undefined:
this.B = new ClassB(this.options);
this.changeOptions();
}
changeOptions() {
// 3. Mutate this.options without changing its reference:
Object.assign(this.options, {
opt1: 1,
opt2: 2,
});
// 3. You can also do it like this:
this.options.opt3 = 3;
}
}
class ClassB {
constructor(options) {
this.options = options;
}
getOptions() {
return this.options;
}
}
const a = new ClassA();
a.changeOptions();
console.log(a.B.getOptions());
我有以下 TypeScript 代码:
class ClassA {
options: ClassOption;
B: ClassB;
constructor() {
this.B = new ClassB(this.options);
this.changeOptions();
}
changeOptions(): void {
const newOptions: ClassOption = new ClassOption("something");
this.options = newOptions;
}
}
class ClassB {
options: ClassOption;
constructor(opts: ClassOptions) {
this.options = opts;
}
someFunction(): ClassOption {
return this.options;
}
}
问题是当我实例化时 ClassA
:
const a = new ClassA();
a.B.someFunction();
returns undefined
而不是从 ClassA
的 changeOptions
方法设置的新选项。
当在 ClassA
的构造函数中时你做:
this.B = new ClassB(this.options);
this.options
仍然是 undefined
,所以基本上当在 ClassB
的构造函数中你做:
this.options = opt;
您只是将 this.options
设置为 undefined
而不是为其分配对 ClassA
的 options
的引用,后者不存在,因为它不存在已初始化。
即使您在 ClassA
中用空对象初始化 options
,如果您为它分配 (this.options = something
) 一个新值,ClassB
也不会引用新值。
你想做的是:
用空对象初始化
ClassA
的this.options
:options: ClassOption = {};
将其传递给
ClassB
的构造函数。此处无需更改。调用
ChangeOptions
时,改变同一个对象而不是用新对象替换它。您可以使用Object.assign
合并两个对象:changeOptions(): void { const newOptions: ClassOption = new ClassOption("something"); Object.assign(this.options, newOptions); // Note that after this, `this.options`' reference is preserved. }
在这里你可以看到它以普通的方式工作 JavaScript:
class ClassA {
constructor() {
// 1. Initialise with an empty object:
this.options = {};
// 2. Pass that reference to ClassB instead of undefined:
this.B = new ClassB(this.options);
this.changeOptions();
}
changeOptions() {
// 3. Mutate this.options without changing its reference:
Object.assign(this.options, {
opt1: 1,
opt2: 2,
});
// 3. You can also do it like this:
this.options.opt3 = 3;
}
}
class ClassB {
constructor(options) {
this.options = options;
}
getOptions() {
return this.options;
}
}
const a = new ClassA();
a.changeOptions();
console.log(a.B.getOptions());