Skip to content

Commit

Permalink
doc: remove the buffer example using Symbol.toPrimitive
Browse files Browse the repository at this point in the history
The single buffer API is the only one supporting Symbol.toPrimitive
but ideally it should not be used. Remove the documentation to
prevent users from potentially using this API.
  • Loading branch information
BridgeAR committed Jan 12, 2020
1 parent 1ed3193 commit 99bdafc
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,18 @@ appropriate for `Buffer.from()` variants.

### Class Method: `Buffer.from(object[, offsetOrEncoding[, length]])`
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/27051
description: Dropped support for `Symbol.toPrimitive`.
added: v8.2.0
-->

* `object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`.
* `object` {Object} An object supporting `valueOf()`.
* `offsetOrEncoding` {integer|string} A byte-offset or encoding, depending on
the value returned either by `object.valueOf()` or
`object[Symbol.toPrimitive]()`.
* `length` {integer} A length, depending on the value returned either by
`object.valueOf()` or `object[Symbol.toPrimitive]()`.
the value returned by `object.valueOf()`.
* `length` {integer} A length, depending on the value returned by
`object.valueOf()`.

For objects whose `valueOf()` function returns a value not strictly equal to
`object`, returns `Buffer.from(object.valueOf(), offsetOrEncoding, length)`.
Expand All @@ -902,20 +905,6 @@ const buf = Buffer.from(new String('this is a test'));
// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
```

For objects that support `Symbol.toPrimitive`, returns
`Buffer.from(object[Symbol.toPrimitive](), offsetOrEncoding, length)`.

```js
class Foo {
[Symbol.toPrimitive]() {
return 'this is a test';
}
}

const buf = Buffer.from(new Foo(), 'utf8');
// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
```

A `TypeError` will be thrown if `object` has not mentioned methods or is not of
other type appropriate for `Buffer.from()` variants.

Expand Down

0 comments on commit 99bdafc

Please sign in to comment.