2016-07-06 23 views
3

陣列我從C平移程序++到打字原稿,我面對奇怪的行爲試圖清空使用拼接技術(How do I empty an array in 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或代碼片段效果很好,但對鉻斷言失敗,參數「src」中成爲一個數字(實際上數組的大小),我究竟做錯了什麼? (兩個初始化呼叫模擬處理中的WebGL環)

chromium splice failing

感謝。

回答

1

它看起來像splice,它創建一個新的數組來返回已刪除的元素,重用它所調用的元素的類,從而調用具有所需大小的自定義構造函數。

在這裏,您可以修復使用其他方式來清空一個數組的問題:它的大小設置爲0。更換

this.splice(0, this.length); 

this.length = 0; 

另一種解決方案可能是尊重你作爲擴展Array的構造函數有比你在子類中實現一個不同的行爲類的合同。

注意,你在一個灰色地帶,對於規範。避免擴展像Array這樣的基本類可能更爲明智。

+0

謝謝你的解釋,我沒有注意到這個衝突Array的「arrayLength」構造! 的修復效果很好鉻,但我會按照你的意見,所以我不會有所有擴展Array類,我會可謂不改變設計,以創建一個新的陣列,而不是從擴展的類內排空。 –