Q:

Can I eval expression in object context?

Good day. I need to eval expression in some object context, but the only solution I found is to create stubs for every object function:
 var c = {
    a : function () {
      return 'a';
    },
    b : function () {
      return 'b';
    }
  };

  function evalInObj(c, js) {
    function a() {
      return c.a();
    }

    function b() {
      return c.b();
    }

    return eval(js);
  };

  console.log(evalInObj(c, 'a() + b()'));

Show me the right way, please. Can I do it with prototype?
  var C = function(id) {
    this.id = id;
  }

  C.prototype.a = function () {
    return 'a' + this.id;
  }

  C.prototype.b = function () {
    return 'b' + this.id;
  }

  function evalCtx(js) {
    console.log(this);  // C {id: 1}
    return eval(js);
  }

  var c1 = new C(1);
  evalCtx.call(c1, 'a() + b()'); // error: a is not defined

A:

function runScript(ctx, js){ with(ctx){ return eval(js); }}

closed. thanks all

