From: kumavis Date: Sun, 11 Aug 2019 23:22:37 +0000 (+0800) Subject: use Object.setPrototypeOf instead of __proto__ X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=db5572c747f3f73ea84ac85729c7527db7016ec2;p=buffer.git use Object.setPrototypeOf instead of __proto__ --- diff --git a/index.js b/index.js index cee72d8..52ec240 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,9 @@ function typedArraySupport () { // Can typed array instances can be augmented? try { var arr = new Uint8Array(1) - arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } } + var proto = { foo: function () { return 42 } } + Object.setPrototypeOf(proto, Uint8Array.prototype) + Object.setPrototypeOf(arr, proto) return arr.foo() === 42 } catch (e) { return false @@ -75,7 +77,7 @@ function createBuffer (length) { } // Return an augmented `Uint8Array` instance var buf = new Uint8Array(length) - buf.__proto__ = Buffer.prototype + Object.setPrototypeOf(buf, Buffer.prototype) return buf } @@ -177,8 +179,8 @@ Buffer.from = function (value, encodingOrOffset, length) { // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: // https://github.com/feross/buffer/pull/148 -Buffer.prototype.__proto__ = Uint8Array.prototype -Buffer.__proto__ = Uint8Array +Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype) +Object.setPrototypeOf(Buffer, Uint8Array) function assertSize (size) { if (typeof size !== 'number') { @@ -282,7 +284,8 @@ function fromArrayBuffer (array, byteOffset, length) { } // Return an augmented `Uint8Array` instance - buf.__proto__ = Buffer.prototype + Object.setPrototypeOf(buf, Buffer.prototype) + return buf } @@ -1095,7 +1098,8 @@ Buffer.prototype.slice = function slice (start, end) { var newBuf = this.subarray(start, end) // Return an augmented `Uint8Array` instance - newBuf.__proto__ = Buffer.prototype + Object.setPrototypeOf(newBuf, Buffer.prototype) + return newBuf }