0

Is it possible to access the previous scope of a function in Javascript? Or is it possible to automaticaly assign "this" to a parameter in typescript when calling a function? (so I have the previous scope)

for example: (a Function defined in my class Queryable)

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

and I use it from outside:

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

now inside my Where function, I need to get the value of "jj"

Mabey a little bit more Code helps:

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();

And inside the "Where" I need to access the Variables wich were used in the function in his parameter

Jochen Kühner
  • 1,385
  • 2
  • 18
  • 43
  • 1
    Your question is difficult to understand, but the variable `jj` should be visible in that second piece of code. – Pointy Mar 12 '16 at 14:36
  • It is visible if you execute the lambda in "filter", but I want to parse it, and send it over the network, and for this I need the value of the closure outside of the lambda – Jochen Kühner Mar 12 '16 at 14:40
  • what's the acorn.parse supposed to achieve? you can just call the filter with filter() function and it will get executed with the right context. – toskv Mar 12 '16 at 14:48
  • No, I need to pass the function to a method on my server and execute it there! For this I need to include the values of the outer variables in the function string! – Jochen Kühner Mar 12 '16 at 15:11
  • what you are sending to the server is just the string representation of the given function. In order for it to continue execution on the server you would need to somehow send the entire execution context for that function (which is not possible) and continue the execution on the server side. So.. what you want is not possible. :) – toskv Mar 12 '16 at 16:21
  • **not possible** might be a little bit too strong. It is possible. You would have to parse the function as the interpreter would and include all it's dependencies in some format the server can later decode and reconstruct the execution context. Keep in mind dependencies maybe anything, variables, function calls, new function definitions anything. :) – toskv Mar 12 '16 at 16:27

1 Answers1

0

I do not think its possible to access 'previous' scope except for the case when its global scope (see more here). And even if it where possible and you could get reference to 'this' it will not allow you to access variables.

I would recommend to rework your approach and use instead of variables - objects of classes with properties designed to hold all required info. Then you will be able to pass instances of such parent object along with function calls and access its properties as needed.

Roughly like this:

class Queryable<T> 
{
    public Where(parent: any, filter: (part: T) => boolean): IQueryable<T> 
    {
        var jj = parent.jj;
        var ast = acorn.parse("(" + filter.toString() + ")");
        return null;
    }
}

class Test 
{
    public jj;

    public Run()
    {
        this.jj = "bb";
        let aa = new Queryable<ITest>();
        aa.Where(this, x => x.content == "aa" && x.bb.content == this.jj || x.form.Contains("Hallo"));
    }
}
Community
  • 1
  • 1
Amid
  • 21,508
  • 5
  • 57
  • 54
  • The problem is, I wanted to keep the same syntax we used in WPF/Silverlight! I know I can achieve it in another way, but all the other ways are more complex! Maybe I'll then use extra parameters for that, if it's not possible in this way – Jochen Kühner Mar 12 '16 at 16:13
  • @Amid while this solution would work, it looks like the Where function is part of a generic library, and it shouldn't know about specific filters. Also what he wants to do is get the function and execute it on the server which is not really possible. Or advisable, it's a sure way to put a security whole in your website. :) – toskv Mar 12 '16 at 16:24
  • Completely agree. I have not said that it is the best approach. Maybe I was not clear enough and should have put emphasis on 'I would recommend to rework your approach'. My sample is rough and could not be used in production. Only as illustration of possibilities to get the required behavior. – Amid Mar 12 '16 at 17:12
  • It would not be a Security hole. In my C# Backend I use already a Filtered IQueryable's wich have only Data wich the User is allowed to access. I now want that the We could use them the same way we do in Silverlight/WPF. I could also achieve this by query them by strings, but I would do it in a type safe way, so it's easier to develop! – Jochen Kühner Mar 13 '16 at 13:49