我想不参考地复制Javascript Error
。
示例:
const error1 = new Error('error1');
const error2 = error1;
error2.message = 'error2';
console.log(error1);
console.log(error2);
输出:
Error: error2
at evalmachine.<anonymous>:1:16
at Script.runInContext (vm.js:133:20)
at Object.runInContext (vm.js:311:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at ReadStream.Readable.push (_stream_readable.js:224:10)
at lazyFs.read (internal/fs/streams.js:181:12)
Error: error2
at evalmachine.<anonymous>:1:16
at Script.runInContext (vm.js:133:20)
at Object.runInContext (vm.js:311:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at ReadStream.Readable.push (_stream_readable.js:224:10)
at lazyFs.read (internal/fs/streams.js:181:12)
当我更改error2
时,error1
也将更改。然后我尝试了lodash clone
功能。但是不幸的是,它返回一个对象,而不是Error的实例。有没有一种方法可以使我无需引用即可复制Error
并返回Error
的实例而无需更改stack
或其他默认属性?