Feature Request : Access Closures of a function, outside of the Function!

# Jochen Kuehner (9 years ago)

Hello,

I’m trying to implement a Feature like „ExpressionTrees“ wich I have in C# in Javascript. This all would work very well, because I could use the String Representation of my function, to build a Expression Tree of it! The Problem only is, I could not access the closures of that function. They are only available when I execute it (what I would not do) (If I wanted to that I need not the Expression Tree of my function)

So I’ll suggest that we introduce a way, so I can access closures of a function from outside:

Example: (it’s TypeScript Code but in Javascript it’s the same error) Inside my „Where“ function, I’d like to access the closures of the function wich I have in the „filter“ parameter…

class Queryable<T> {

Where(filter: (part: T) => boolean): IQueryable<T> {
    var ast = acorn.parse("(" + filter.toString() + ")");

    return null;
}

}

interface String { Contains(par: String): boolean; StartsWith(par: String): boolean; EndsWith(par: String): boolean; }

interface ITest { content: string; form: string; href: string; bb: ITest; cc: ITest[]; }

function test() { var aa = new Queryable<ITest>(); var jj = "bb"; aa.Where(x => x.content == "aa" && x.bb.content == jj || x.form.Contains("Hallo")); }

test();

Jochen Kühner E-Mail jochen.kuehner at kardex.com<mailto:jochen.kuehner at kardex.com>

[cid:image001.png at 01D02A98.EF2B1CC0]

MLOG Logistics GmbH Wilhelm-Maybach-Str.2 74196 Neuenstadt am Kocher Germany Telephone

+49 (7139) 4893252

Telefax

+49 (7139) 489399328

www.kardex-mlog.com, www.kardex-mlog.com

# Gorgi Kosev (9 years ago)

Have you tried experimenting with babel to achieve this? Here is a starting point for a transform that makes all arrow functions behave this way: gist.github.com/spion/adc5b0110f9466efc0a2 (note: may need more work)

Unfortunately, as far as I know whole new type of function would probably have to be introduced for this. Some security capabilities of JS are based on closure captures being inaccessible outside their scope.

# Isiah Meadows (9 years ago)

Oh, and closures being accessible outside of them also would have huge memory implications. IMHO that's when you start just using traditional OOP, and bind methods where necessary.