Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't structuredClone copy Function Object?

Tags:

javascript

MDN says that "Function objects cannot be duplicated by the structured clone algorithm; attempting to throws a DataCloneError exception."

Why does copying a function object emit an error with structuredClone()?

like image 378
Byeongin Yoon Avatar asked Apr 01 '26 18:04

Byeongin Yoon


1 Answers

If you read on MDN/structuredClone, it says

Exceptions

DataCloneError DOMException

  • Thrown if any part of the input value is not serializable.

For why functions are not serializable, see here: Why is a function not serializable?

From what I understand of those answers, the TL;DR is that it's because a function can be converted to a string, but the variables/functions referenced in the function that are out of scope would cause issues.

like image 151
Samathingamajig Avatar answered Apr 03 '26 09:04

Samathingamajig