tc39 / proposal-bind-operator

This-Binding Syntax for ECMAScript
1.75k stars 30 forks source link

This proposal sounds as in-language replacement of (0, expr) hacky syntax? #57

Open koteisaev opened 2 years ago

koteisaev commented 2 years ago

Hello! Lately I was intrigued with the need to use syntax like (0, expr) for imports from modules to make sure it does not called on module import as this. So, what exactly happen if this proposal will be adopted, and following construct will be applied:

(undefined::module1.importedFunc)("a", true, 1, ...[1,2,3]);

What if this proposal can be extended to either

module1:>importFunc("a", true, 1, ...[1,2,3]);

or

module1->importFunc("a", true, 1, ...[1,2,3]);

to explicitly say "I want to call importFunc as independent function without relation to module1 import reference", with undefined this?

May be, :> can be used to call member method with undefined this and -> to call some method with left operand of this, like

module1:>importedFunc("a", true, 1, ...[1,2,3]);
//call `importedFunc` with `this` set to `window`
window->module1.importedFunc("a", true, 1, ...[1,2,3]);
//here call `module1.libFactory` first decoupled from `module1` import
window->(module1:>libFactory().importedFunc)("a", true, 1, ...[1,2,3]);

Thanks for reading this.

ljharb commented 2 years ago

That seems like a different proposal, and I’m very skeptical that there’s a use case for it outside of transpiler output, since in actual user code, this is done by destructuring the function in one statement, and calling it in the next.