+ 1
The use of "with" function
I have a js code to deserialize a JS fn from a serialized Function the reason is that I want to transfer it to my web worker it's compatible with web and node so that's good but I found out that it's not recommended to use what do it do is there any alternative? function deserializeFn(serializedObj) { let fn = new Function('with(this) { return ' + serializedObj.value + '; }'); return fn.call(serializedObj.scope); }
3 Respuestas
+ 2
Creative Creations 1
A better approach would be not to use "with"
```
const serializedObj = { value: 'return x + y;' };
function deserializeFn(serializedObj) {
const { value } = serializedObj;
const fn = new Function('x', 'y', value);
return fn;
}
const deserializedFn = deserializeFn(serializedObj);
const result = deserializedFn(5, 3); // result will be 8
document.write(result);
```
0
https://sololearn.com/compiler-playground/Wvf3b6Dsckm5/?ref=app
https://sololearn.com/compiler-playground/ccN2JwwG9IvH/?ref=app
https://sololearn.com/compiler-playground/cUahvmm9lJ7F/?ref=app
https://sololearn.com/compiler-playground/Wp3Nbth84otD/?ref=app
https://sololearn.com/compiler-playground/WaRkN97dU30s/?ref=app
https://sololearn.com/compiler-playground/WyldSd8zH0jW/?ref=app
https://sololearn.com/compiler-playground/c32Oa7tX1k3f/?ref=app
https://sololearn.com/compiler-playground/c5HThgBJh1Eh/?ref=app
https://sololearn.com/compiler-playground/c9craT2MZ5QB/?ref=app
https://sololearn.com/compiler-playground/WNW1xgQ9NKpQ/?ref=app
https://sololearn.com/compiler-playground/Wh0c485WAk4v/?ref=app
0
Godwin Emmydonj please do not spam threads .. follow the forum guidelines
https://www.sololearn.com/discuss/1316935/?ref=app
https://www.sololearn.com/discuss/3021159/?ref=app