@@ -547,11 +547,6 @@ const otherChannel = new MessageChannel();
547
547
port2 .postMessage ({ port: otherChannel .port1 }, [ otherChannel .port1 ]);
548
548
```
549
549
550
- Because the object cloning uses the structured clone algorithm,
551
- non-enumerable properties, property accessors, and object prototypes are
552
- not preserved. In particular, [ ` Buffer ` ] [ ] objects are read as
553
- plain [ ` Uint8Array ` ] [ ] s on the receiving side.
554
-
555
550
The message object is cloned immediately, and can be modified after
556
551
posting without having side effects.
557
552
@@ -606,6 +601,49 @@ The `ArrayBuffer`s for `Buffer` instances created using
606
601
transferred but doing so renders all other existing views of
607
602
those ` ArrayBuffer ` s unusable.
608
603
604
+ #### Considerations when cloning objects with prototypes, classes, and accessors
605
+
606
+ Because object cloning uses the [ HTML structured clone algorithm] [ ] ,
607
+ non-enumerable properties, property accessors, and object prototypes are
608
+ not preserved. In particular, [ ` Buffer ` ] [ ] objects will be read as
609
+ plain [ ` Uint8Array ` ] [ ] s on the receiving side, and instances of JavaScript
610
+ classes will be cloned as plain JavaScript objects.
611
+
612
+ ``` js
613
+ const b = Symbol (' b' );
614
+
615
+ class Foo {
616
+ #a = 1 ;
617
+ constructor () {
618
+ this [b] = 2 ;
619
+ this .c = 3 ;
620
+ }
621
+
622
+ get d () { return 4 ; }
623
+ }
624
+
625
+ const { port1 , port2 } = new MessageChannel ();
626
+
627
+ port1 .onmessage = ({ data }) => console .log (data);
628
+
629
+ port2 .postMessage (new Foo ());
630
+
631
+ // Prints: { c: 3 }
632
+ ```
633
+
634
+ This limitation extends to many built-in objects, such as the global ` URL `
635
+ object:
636
+
637
+ ``` js
638
+ const { port1 , port2 } = new MessageChannel ();
639
+
640
+ port1 .onmessage = ({ data }) => console .log (data);
641
+
642
+ port2 .postMessage (new URL (' https://example.org' ));
643
+
644
+ // Prints: { }
645
+ ```
646
+
609
647
### ` port.ref() `
610
648
<!-- YAML
611
649
added: v10.5.0
0 commit comments