簡體   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