Is it possible to get a function's scope chain?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DanYan

    Is it possible to get a function's scope chain?

    So I was doing some stuff in Javascript, and I want to get access to a
    function's scope chain. As a simplified example of what I actually am
    trying to do, suppose I have this:


    function add(b) {
    return function(a) {
    return a + b;
    };
    }

    var add3 = add(3);
    assert(5 == add3(2));


    I want to be able to define a function mul3() that executes in the
    same context as bar. I want to do something like this:


    function mul3() {
    return a * b; // I realize that no b is in static scope here -
    see below
    }
    mul3.scope = add3.scope;

    assert(6 == mul3(2));


    It doesn't appear that functions have a scope property. Is there any
    way at all to do this? Is there any theoretically standards-compliant
    way to do it? Thanks!
  • Thomas 'PointedEars' Lahn

    #2
    Re: Is it possible to get a function's scope chain?

    DanYan wrote:
    So I was doing some stuff in Javascript, and I want to get access to a
    function's scope chain.
    You are trying to recreate the execution context in which a Function
    object was created instead.
    As a simplified example of what I actually am trying to do, suppose I have this:
    >
    function add(b) {
    return function(a) {
    return a + b;
    };
    >
    }
    >
    var add3 = add(3);
    assert(5 == add3(2));
    >
    I want to be able to define a function mul3() that executes in the
    same context as bar.
    Since the execution context in which the anonymous Function object was
    created does not exist anymore, I doubt that is possible.
    I want to do something like this:
    >
    function mul3() {
    return a * b; // I realize that no b is in static scope here -
    see below}
    >
    mul3.scope = add3.scope;
    >
    assert(6 == mul3(2));
    >
    It doesn't appear that functions have a scope property.
    Correct.
    Is there any way at all to do this? Is there any theoretically
    standards-compliant way to do it?
    There is a practically standards-compliant way to do it: native
    objects.

    function Operand(a)
    {
    this.value = a;
    }

    Operand.prototy pe = {
    add: function(b) {
    return this.value + b;
    }
    };

    var three = new Operand(3);

    assert(5 === three.add(2));

    // new properties can be added to native objects on the fly
    Operand.prototy pe.mul = function(b) {
    return this.value * b;
    };

    assert(6 === three.mul(2));

    // user-defined properties of native objects can be deleted again
    delete Operand.prototy pe.mul;

    // TypeError: three.mul is not a function
    assert(6 === three.mul(2));


    HTH

    PointedEars

    Comment

    • VK

      #3
      Re: Is it possible to get a function's scope chain?

      On Jun 5, 3:43 am, DanYan <bytecraf...@gm ail.comwrote:
      So I was doing some stuff in Javascript, and I want to get access to a
      function's scope chain. As a simplified example of what I actually am
      trying to do, suppose I have this:
      >
      function add(b) {
      return function(a) {
      return a + b;
      };
      >
      }
      >
      var add3 = add(3);
      assert(5 == add3(2));
      >
      I want to be able to define a function mul3() that executes in the
      same context as bar. I want to do something like this:
      >
      function mul3() {
      return a * b; // I realize that no b is in static scope here -
      see below}
      >
      mul3.scope = add3.scope;
      >
      assert(6 == mul3(2));
      >
      It doesn't appear that functions have a scope property. Is there any
      way at all to do this? Is there any theoretically standards-compliant
      way to do it? Thanks!
      "theoretica lly standards-compliant" would be not using pseudo-
      inheritance over closures. I consider it absolutely awful and ugly way
      to do things from any OOP point of view, either class-based or
      prototype-based. Yet mutable closure-constructors are still in some
      fashion - though thanks Gog lesser and lesser - so you may and
      probably will disregard this advise.

      Fot your question itself you may use call() and apply() methods to get
      back into the MCC:

      function outer(arg_outer ) {
      return function inner(arg_inner , overload) {
      if (!!overload) {
      arg_outer = overload;
      }
      return arg_outer + arg_inner;
      };
      }

      var demo = outer(1);
      window.alert(de mo(2)); // 3

      window.alert(de mo.call(outer,2 ,2)); // 4

      window.alert(de mo(2)); // 4

      Comment

      Working...