-2

I have a string like this:

var jsontext = 'function(prog) {return ' + path + ';}';

and i want to parse this into javascript.

How can i do this?

Thomas

n-dru
  • 9,285
  • 2
  • 29
  • 42
Thomas
  • 11
  • 3

3 Answers3

0

This is not JSON, but Javascript as a text string that you want to evaluate.

You will want to use the eval(jsontext) function, but I would highly discourage this as it is a very risky function as it executes the script with the privileges of the caller. It is also a bad idea for performance reasons. If at all possible, define your function in the script itself instead of as a string!

See here.

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
  • My problem is that the next function use this function as parameter. – Thomas Apr 13 '15 at 09:49
  • var tester = buildFilter(node, selector); selector is the function with the path. – Thomas Apr 13 '15 at 09:51
  • Can't you just pass the `path` variable to the next function and create the function within the next function, so you don't have to pass through code? If you can post these two functions I will try to find a solution for you. – 08Dc91wk Apr 13 '15 at 09:58
0

Here the complete code. The eval function doesn't work and i need a workaround.

function buildRecursiveFilter(filterDef) { var filterFuncs = [];

    // Rekursiv für alle nodes Regex filter funktionen definieren
    naw.visitNode(filterDef, function(node, path) {

        if (typeof node !== "object") {
          var selector = eval("(function(TBM) { return " + path.toUpperCase() + "; })") ;
            var tester = buildRegexFilter(node, selector);         
          filterFuncs.push(tester);
        }
    }, "tbm");

    if (filterFuncs.length === 0) return noFilter;

    // wende alle test funktionen auf tbm an und liefere die UND Verknüpfung
    return function(tbm) {
        return _.reduce(
            _.map(filterFuncs, function(f) { return f(tbm); }),
            function(akku, res) { return akku && res; });
    };
}

function buildRegexFilter(filterDef, fieldSelector) {
    fieldSelector = fieldSelector || (function(tbm) { return tbm.WP; });
    var def = filterDef;
    if (typeof (def) === 'string') {
        def = def.split(',');
    }
    var filters = [].concat(def); // Array erzwingen
    filters = _.map(filters, function(s) { return $.trim(s); });
    var expression = '(' + filters.join('|') + ')';
    var rx = expression !== '(.*)' ? new RegExp(expression) : undefined;

    return function (tbm) {
        return rx.test(fieldSelector(tbm));
    };
}

Thomas

Thomas
  • 11
  • 3
  • ich mein das nicht böse aber du solltest wirklich javascript lernen. – GottZ Apr 13 '15 at 10:29
  • Lach, bin doch dabei. Das Script ist auch nicht von mir. Ich versuch nur grad ne Lösung für das Fehlverhalten zu finden – Thomas Apr 13 '15 at 10:34
  • na dann ist ja gut. falls du den kennst der das geschrieben hat dann hau ihm auf die finger. hau dir das mal rein: http://www.codecademy.com/tracks/javascript und das hier: http://books.google.de/books/about/Pro_JavaScript_Design_Patterns.html?id=za3vlnlWxb0C&redir_esc=y – GottZ Apr 13 '15 at 10:35
0

I've found a solution for my problem. I have removed the line with eval and have it replaced with this:

var fn = "function(tbm) { return " + path.toUpperCase() + "; }";
var selector = new Function("return (" + fn + ")")();

Thomas

Thomas
  • 11
  • 3