Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShalvah <shalvah@users.noreply.github.com>2022-01-21 18:38:51 +0300
committerGitHub <noreply@github.com>2022-01-21 18:38:51 +0300
commit087939471a47d7059223a5f52c933d37cbe547a9 (patch)
tree7b8df6e7c5597b7ce3e93af10903f6707c6efd1f /doc/api/buffer.md
parent870044ffa124209f18e82aff4c7bc902fb80e749 (diff)
doc: demonstrate dangers of `buffer.slice()`
PR-URL: https://github.com/nodejs/node/pull/41628 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'doc/api/buffer.md')
-rw-r--r--doc/api/buffer.md16
1 files changed, 16 insertions, 0 deletions
diff --git a/doc/api/buffer.md b/doc/api/buffer.md
index 80a97ababd2..108ed091f5c 100644
--- a/doc/api/buffer.md
+++ b/doc/api/buffer.md
@@ -3427,6 +3427,14 @@ console.log(copiedBuf.toString());
console.log(buf.toString());
// Prints: buffer
+
+// With buf.slice(), the original buffer is modified.
+const notReallyCopiedBuf = buf.slice();
+notReallyCopiedBuf[0]++;
+console.log(notReallyCopiedBuf.toString());
+// Prints: cuffer
+console.log(buf.toString());
+// Also prints: cuffer (!)
```
```cjs
@@ -3441,6 +3449,14 @@ console.log(copiedBuf.toString());
console.log(buf.toString());
// Prints: buffer
+
+// With buf.slice(), the original buffer is modified.
+const notReallyCopiedBuf = buf.slice();
+notReallyCopiedBuf[0]++;
+console.log(notReallyCopiedBuf.toString());
+// Prints: cuffer
+console.log(buf.toString());
+// Also prints: cuffer (!)
```
### `buf.swap16()`