gpt4 book ai didi

javascript - 在 TypeScript 中访问对象的子对象

转载 作者:行者123 更新时间:2023-12-03 08:13:54 24 4
gpt4 key购买 nike

我正在尝试访问 TypeScript 中对象的子对象。

所以object1是Object的子对象。当我控制台日志对象(父级)时,我清楚地看到属性“Children”。如果展开 Children,我会看到 Object1。

enter image description here

通常在 JavaScript 中,我会做类似的事情

var child = Object.children[0];

但是,当我在 TypeScript 中执行此操作时,它会给出语法错误

error TS2339: Property 'children' does not exist on type 'DisplayObject'.

在 TS 中是否有不同的方法?

编辑

示例代码:

  this.game.world.children.forEach(function(child){
var constructorString: string = child.constructor.toString();
var className: string = constructorString.match(/\w+/g)[1];
if(className=='AcheivmentButton'){
for(var i=0; i<child.children.length; i++)
children.push(child.children[i]);
}
})

最佳答案

Is there a different way to do it in TS?

没有。这只是您正在使用的对象的定义存在差异。

最快的方法是简单地使用断言:

var child = (Object as any).children[0];

更多

关于javascript - 在 TypeScript 中访问对象的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com