3 ms·
I'm not up-to-date regarding JS programming, so: why is this solved as it is, i.e. as anonymous function that is immediately called? First idea would be to prev
by _Microft 6y ago
I'm not up-to-date regarding JS programming, so: why is this solved as it is, i.e. as anonymous function that is immediately called? First idea would be to prevent leaking anything into a global context but this shouldn't be an issue here. Can anyone explain?
var supported = (() => {
try {
if (
typeof WebAssembly === "object" &&
typeof WebAssembly.instantiate === "function"
) {
const module = new WebAssembly.Module(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
);
if (module instanceof WebAssembly.Module)
return (
new WebAssembly.Instance(module) instanceof WebAssembly.Instance
);
}
} catch (e) { }
return false;
})();
Edit: is it maybe to save one from having to think about leaking anything, i.e. wrap everything in (() => { ... })(); and call it a day?
- Vanit 6y agoI think this is an attempt to keep the code clean without multiple assignments to 'supported'. I don't think there's any real benefit. Unless something else took a reference to it the GC will get it after the main script has run.
- joway 6y agoHi, actually, the function is referenced from https://stackoverflow.com/questions/47879864/how-can-i-check-if-a-browser-supports-webassembly https://stackoverflow.com/questions/47879864/how-can-i-check... . :( But I think there are two reasons to write like that: 1. It avoids multiple assignments, which could make codes cleaner. 2. My website is too simple so maybe there is no enough reason to do such "optimization". But if we are talking about a big project, this initialized global variable could make other codes which required to check if the wasm feature enabled easier and just need to calculate the status one time.