繁体   English   中英

使用splice用Chromium清空数组

[英]Using splice to empty an array with Chromium

我正在将一个程序从c ++翻译成typescript,我面临一个奇怪的行为,试图使用拼接技术清空一个数组如何在JavaScript中清空一个数组? )来清空一个数组。

这是我在打字稿中的代码的摘录

 "use strict" class UniformGridGeometry<ItemT> extends Array<ItemT> { itemType: { new (): ItemT; } constructor(itemType: { new (): ItemT; }) { // constructor(itemType: { new (): ItemT; }, uGeomTemplate: UniformGridGeometry<any>) // any : Vorton, Particle, Vec*, Mat*, ... // constructor(itemType: { new (): ItemT; }, uNumElements: number, vMin: Vec3, vMax: Vec3, bPowerOf2: boolean) // constructor(itemType: { new (): ItemT; }, arg?: any, vMin?: Vec3, vMax?: Vec3, bPowerOf2?: boolean) { super(); // Array this.itemType = itemType; // (...) } } class UniformGrid<ItemT> extends UniformGridGeometry<ItemT> { constructor(itemType: { new (): ItemT; }) { // constructor(itemType: { new (): ItemT; }, uGeomTemplate: UniformGridGeometry<any>) // any : Vorton, Particle, Vec*, Mat*, ... // constructor(itemType: { new (): ItemT; }, uNumElements: number, vMin: Vec3, vMax: Vec3, bPowerOf2: boolean) // constructor(itemType: { new (): ItemT; }, arg?: any, vMin?: Vec3, vMax?: Vec3, bPowerOf2?: boolean) { super(itemType); // (...) } } class NestedGrid<ItemT> extends Array<UniformGrid<ItemT>> { constructor(src?: UniformGrid<ItemT>) { super(); if (src) { this.Init(src); } } Init(src: UniformGrid<ItemT>) { this.splice(0, this.length) // mUniformGrids.Clear() ; console.assert(typeof src === 'object', typeof src); // let numUniformGrids = this.PrecomputeNumUniformGrids( src ) ; // this.mUniformGrids.Reserve( numUniformGrids ) ; // Preallocate number of UniformGrids to avoid reallocation during PushBack. let uniformGrid = new UniformGrid<ItemT>(src.itemType); // uniformGrid.Decimate( src , 1 ) ; // uniformGrid.Init() ; this.push(uniformGrid); // (...) } } function doTests() { console.info("Test > NestedGrid ; UniformGrid"); let mInfluenceTree: NestedGrid<any> = new NestedGrid<any>(); // Influence tree let ugSkeleton = new UniformGrid<any>(null); mInfluenceTree.Init(ugSkeleton); console.log(mInfluenceTree); mInfluenceTree.Init(ugSkeleton); console.log(mInfluenceTree); } doTests(); 

生成(ES6目标)以下Javascript:

 "use strict"; class UniformGridGeometry extends Array { constructor(itemType) { super(); this.itemType = itemType; } } class UniformGrid extends UniformGridGeometry { constructor(itemType) { super(itemType); } } class NestedGrid extends Array { constructor(src) { super(); if (src) { this.Init(src); } } Init(src) { this.splice(0, this.length); console.assert(typeof src === 'object', typeof src); let uniformGrid = new UniformGrid(src.itemType); this.push(uniformGrid); } } function doTests() { console.info("Test > NestedGrid ; UniformGrid"); let mInfluenceTree = new NestedGrid(); let ugSkeleton = new UniformGrid(null); mInfluenceTree.Init(ugSkeleton); console.log(mInfluenceTree); mInfluenceTree.Init(ugSkeleton); console.log(mInfluenceTree); } doTests(); 

相同的代码,在firefox或代码片段上运行良好,但在chrome上断言失败,参数'src'变成一个数字(实际上是数组的大小)我做错了什么? (两个Init调用模拟WebGL循环中的处理)

铬拼接失败

谢谢。

它看起来像splice ,它创建一个新数组来返回已删除的元素,重用它所调用的元素的类,从而调用具有所需大小的自定义构造函数。

在这里,您可以使用另一种方法清空数组来解决问题:将其大小设置为0 更换

this.splice(0, this.length);

this.length = 0;

另一种解决方案可能是尊重您扩展的类的契约,因为Array的构造函数具有与您在子类中实现的行为不同的行为。

请注意,关于规格,您处于灰色区域。 避免扩展像Array这样的基本类可能更明智。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM