首页 > 解决方案 > 将 dword 类型实现为 Javascript 数组的子类?

问题描述

我需要实现扩展精度布尔二进制算术,我想我可以使用类来为 DWORD 和“MULTIWORD”创建类型。

这样做有什么问题吗?似乎正在工作。

PS 我宁愿留在 ES5 (pre class ) 范式内。

// not production code
var DoubleWord = JSClass.Extends(Array, function(initx) {
    DoubleWord.baseConstructor.call(this, initx);

    // offset naught padding
    var initoffset = 0;

    this.s = 32; // (s)ize in bits

    if (initx)
        for (var i = 0; i < initx.length; i++)
            this.push(initx[i]);

    initoffset = this.length; // pointless, descriptional

    for (var i = initoffset; i < this.s; i++)
        this.push(0);
});

(忽略这JSClass件事) 这里是 JSClass.Extends 继承实现

JSClass = {};

JSClass.Extends = function(baseClass, constructor) {
    Inheritance = function() {};

    Inheritance.prototype = baseClass.prototype;
    constructor.prototype = new Inheritance();
    constructor.prototype.constructor = constructor;
    constructor.baseConstructor = baseClass;
    constructor.superClass = baseClass.prototype;

    return constructor;
};

标签: javascriptarraysinheritance

解决方案


推荐阅读