【发布时间】:2019-02-12 12:11:04
【问题描述】:
最近我在我的代码中发现了一个障碍。我正在使用我的自定义对象和它们的数组。我发现一种情况下 push() 方法有效,另一种情况无效。
第一种情况(工作正常):
class MyObject{
private reference: d3.Selection<SVGElement>;
public constructor(ref: d3.Selection<SVGElement>){
this.reference = ref;
}
}
interface ViewModel{
objects: MyObject[]
}
class MyApp{
private root: d3.Selection<SVGElement>
private viewModel: ViewModel;
constructor(options: Type){
this.root = options.root
this.viewModel.objects.push(new MyObject(this.root))
}
}
第二种情况(不工作):
class MyObject{
private reference: d3.Selection<SVGElement>;
public constructor(ref: d3.Selection<SVGElement>){
this.reference = ref;
}
}
class MyApp{
private root: d3.Selection<SVGElement>
private objects: MyObject[];
constructor(options: Type){
this.root = options.root
this.objects.push(new MyObject(this.root)) //seems to freeze the whole program
}
}
我做错了什么? 任何帮助将不胜感激。
迈克尔
【问题讨论】:
标签: arrays typescript object interface