首页 > 解决方案 > 扩展类 SharedArrayBuffer 导致“构造函数 SharedArrayBuffer 需要‘新’”

问题描述

我正在尝试扩展SharedArrayBuffer该类以轻松传递组件数据以在工作线程中使用,因为我们每秒处理数万个实体。这是我目前的代码。

export default abstract class Component extends SharedArrayBuffer {
  public name: String; 
  public data: DataView;

  public constructor(size) {
      super(size); 
      this.data = new DataView(this); 
      this.name = this.constructor.name.toLowerCase(); 
  }
}

然后我有一个扩展抽象类组件的组件,称为Position.

export default class Position extends Component {
    public constructor(x: number = 0, y: number = 0, layer: number = 0) {
        super(12); 
        this.data.setUint32(0, x);
        this.data.setUint32(4, y);
        this.data.setUint32(8, layer);
    }

    get x() { return this.data.getUint32(0); }
    set x(value: number) { this.data.setUint32(0, value); }

    get y() { return this.data.getUint32(4); }
    set y(value: number) { this.data.setUint32(4, value); }

    get layer() { return this.data.getUint32(8); }
    set layer(value: number) { this.data.setUint32(8, value); }
}

通过创建 Position 组件时,new Position(...)我收到以下错误:

Component.js:19 var _this = _super.call(this, size) || 这个; ^

TypeError:构造函数 SharedArrayBuffer 需要“新”

您可以在下面找到从 TypeScript 代码创建的 JavaScript。

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Component = /** @class */ (function (_super) {
    __extends(Component, _super);
    function Component(size) {
        var _this = _super.call(this, size) || this;
        _this.data = new DataView(_this);
        _this.name = _this.constructor.name.toLowerCase();
        return _this;
    }
    return Component;
}(SharedArrayBuffer));
exports.default = Component;

确保在super执行时调用 SharedArrayBuffer 的构造函数以防止此错误的最佳方法是什么?

标签: javascripttypescript

解决方案


推荐阅读