A way to construct Functions with custom scopes?

# #!/JoePea (5 years ago)

For example, what if we could write something like

function foo() {
  var x = 10
}

const [scope, returnValue] = scope from foo()

// can't do anything with `scope` directly, it's like an empty object
(an exotic object?).
console.log(getOwnAndInheritedKeys(scope)) // empty array

new Function('console.log(x)', { scope }) // logs 10

Or something.

Maybe we can pass objects, and the keys are used for scope, similar to with, but they are passed by reference into the scope:

new Function('console.log(x)', { scope: { x: 10 } }) // logs 10

I think this would open up the doors to frameworks that want to create expressions in things like Element attributes, to make it easy for those expression to have access to certain variables. Currently it is difficult to make this happen; the cleanest ways of doing it involve build steps. With this idea we would not need build steps to have a clean way of doing it.

#!/JoePea

# Bergi (5 years ago)

It's a bit unclear to me what problem you are trying to solve, but you can already construct closures with custom scopes using the Function constructor:

function makeFunction(name, args, body, scope, values) {
    if (typeof args == "string")
        values = scope, scope = body, body = args, args = [];
    if (!Array.isArray(scope) || !Array.isArray(values)) {
        if (typeof scope == "object") {
            values = Object.values(scope);
            scope = Obect.keys(scope);
        } else {
            values = [];
            scope = [];
        }
    }
    return Function(scope, `
        function ${name}(${args.join(", ")}) {
            ${body}
        }
        return ${name};
    `)(...values);
};

const foo = makeFunction("foo", [], "console.log(x)", {x: 10})
foo(); // logs 10

kind , Bergi

# Steve Fink (5 years ago)

On 6/10/20 11:06 AM, #!/JoePea wrote:

For example, what if we could write something like

function foo() {
   var x = 10
}

const [scope, returnValue] = scope from foo()

// can't do anything with `scope` directly, it's like an empty object
(an exotic object?).
console.log(getOwnAndInheritedKeys(scope)) // empty array

new Function('console.log(x)', { scope }) // logs 10

-1 from me. I think it would be disastrous for performance. It prevents any escape analysis and resulting optimizations. It prevents constant propagation. It might even interfere with inlining. It would add more points where JITs might need to invalidate their compiled code. In general, it eliminates much of the remaining freedom that JS engines have to optimize in the face of the wildly dynamic nature of JavaScript code.

I didn't understand your use case with Element attributes.

Also, how do you specify which scope you want?


     function foo() {

         // Scope 1, with y

         let x = 1;

         // Scope 2, with x and y

         var y = 2;

         // Scope 3

         if (...) {

             let z = 3; // Scope 4, with x, y, and z

          }

     }

etc. And with default parameters and things, there are a lot of scopes to choose from. I won't even explore the messiness of temporal dead zones.

To make it at all practical, I think you'd need to somehow statically declare which functions to permit this for. But at that point, you're better off creating and storing a closure at the exact points where you want to capture scopes (which fixes the "which scope" problem as well.) And it sounds like that wouldn't work for what you want.