使用splice清空Chromium

时间:2016-07-06 09:31:34

标签: javascript typescript

我正在将程序从c ++翻译成typescript,我遇到了一个奇怪的行为,试图使用拼接技术(How do I empty an array in JavaScript?)清空数组以清空数组。

以下是我在nickcript中的代码的摘录

"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循环中的处理)

chromium splice failing

感谢。

1 个答案:

答案 0 :(得分:1)

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

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

this.splice(0, this.length);

this.length = 0;

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

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