ES6类和扩展内置缓冲区

我尝试在Nodejs中扩展缓冲区对象

TypeError: Buffer2.from(...).slice2 is not a function 

但看起来我从来没有不能用新课程来缠绕

我如何使Buffer2.from返回Buffer2?

ES6类和扩展关键字

无论如何更好的方式,而不是一个全新的class级?

 class Buffer2 extends Buffer { constructor(obj, encoding) { console.log('Buffer2::constructor'); super(obj, encoding); return this; } slice(start, end) { console.log('Buffer2::slice'); return this.slice(start, end) } slice2(start, length) { console.log('Buffer2::slice2'); return this.slice(start, start + length) } static from(obj, encoding) { console.log('Buffer2::from'); return new Buffer2(obj, encoding); } } Buffer2.prototype.slice = function (start, end) { console.log('Buffer2::slice'); return this.slice(start, end) } Buffer2.prototype.slice2 = function (start, length) { return this.slice(start, start + length) } console.log(Buffer2, Buffer2.from('7E1D2C', 'hex').slice2(0, 1)); 

缓冲区是作为一个函数导出的,而不是类,在这里你可以看到源代码buffer.js 。 所以,您尝试使用ES6类inheritance而不是es6类,而是运行。 如果你想扩展本地模块 – 使用原型inheritance。 但是我的build议是,当你尝试添加一些function到本地的modiles时,使用inheritance的组成insead。