Node v4.0.0附带一个基于Javascript内置Uint8Array的刷新Buffer类。
据我所知,这是继承的方式:
Buffer.prototype.__proto__ = Uint8Array.prototype;
Buffer.__proto__ = Uint8Array;
第一个问题:为什么 Buffer .__ proto __ 被设置为Uint8Array构造函数?我之前从未见过,我也没有真正得到它。
然后,我尝试升级node-ogg模块,并且有一个类继承自Buffer类:
function ogg_packet (buffer) {
if (!Buffer.isBuffer(buffer)) {
buffer = new Buffer(binding.sizeof_ogg_packet);
}
buffer.__proto__ = ogg_packet.prototype;
return buffer;
}
inherits(ogg_packet, Buffer);
(source)
当我执行测试时,它会无声地失败。 当我在测试执行期间尝试打印新创建的ogg_packet时,我收到了错误:
Uncaught TypeError: argument should be a Buffer
这是我的调试代码:
packet = new ogg_packet();
console.log('PACKET', packet);
此外,here is the upgrade branch to nan v2 (and node v4)
所以我的第二个问题:我怎样才能使它适用于新的Buffer实现?
由于
更新: 我试图改变ogg_packet的构造函数的工作方式:
function ogg_packet (buffer) {
if (!Buffer.isBuffer(buffer)) {
Buffer.call(this, binding.sizeof_ogg_packet);
} else {
Buffer.call(this, buffer);
}
if (this.length != binding.sizeof_ogg_packet) {
throw new Error('"buffer.length" = ' + this.length + ', expected ' + binding.sizeof_ogg_packet);
}
}
inherits(ogg_packet, Buffer);
但是,在尝试访问 this.length 时,我收到了错误:
Uncaught TypeError: Method Uint8Array.length called on incompatible receiver [object Object]