将dword类型实现为Javascript数组的子类?
我需要实现扩展精度布尔二进制算法,我想我可以使用类来创建DWORD和“多字”的类型 这样做有什么不对吗?看起来很有效 另外,我更喜欢ES5(课前)模式将dword类型实现为Javascript数组的子类?,javascript,arrays,inheritance,Javascript,Arrays,Inheritance,我需要实现扩展精度布尔二进制算法,我想我可以使用类来创建DWORD和“多字”的类型 这样做有什么不对吗?看起来很有效 另外,我更喜欢ES5(课前)模式 // not production code var DoubleWord = JSClass.Extends(Array, function(initx) { DoubleWord.baseConstructor.call(this, initx); // offset naught padding var inito
// 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
这件事”-嗯,是的,这是看起来错误的部分。特别是因为你特别询问了Array
子类化。有些气味:a)不清楚“精确布尔运算”是什么意思,但如果它意味着创建大量布尔值数组,那么听起来效率很低b)为什么要将initx
传递给基构造函数?c) .s
不是一个好名字d)如果initx
长度超过32,代码可能不会执行您想要的操作e)initoffset
变量显示为无点感谢添加JSClass.Extends
code。这种继承方式已经过时了……而且。如果您确实想要/需要子类化数组
,请使用ES6类
syntax@Hatebit按位布尔运算符可用于BigInt
。它们几乎肯定比用JS本身编写的任何东西都要快。@Hatebit如果你不完全理解那篇文章,那么请注意数组子类化需要class
语法的建议。如果您想了解有关原型机制的更多信息,请使用对象。在扩展的代码中创建,并使用它从一些非内置类继承。
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;
};