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