Skip to content

Variables declared in outer function can not be cached (v1.2) #1

Closed

Description

@jingwood

Variables declared in outer function but used in inner function can not be cached so the following code does not work in ReoScript.

fun1 = function() { 
  var cached_var = 10;

  return function() {
    console.log('cached_var = ' + cached_var);
  };
} ();

fun1();

The result is that cached_var is null.

Activity

  1. jingwood commented on Jun 11, 2013

    @jingwood
    MemberAuthor

    Functions will cache the call-scope where function was defined in. This issue has been fixed and planned to be available since v1.3.

    Now overriding a function by 'apply' method will be available. (The original code of following sample was published in StackOverflow http://stackoverflow.com/questions/9134686/adding-code-to-a-javascript-function-programatically)

    var t = debug.assert;
    
    t( Function.prototype.hasOwnProperty('apply') );
    
    var a = 0;
    
    function fun1() {
         a += 10;
    }
    
    fun1();
    t(a, 10);
    
    fun1 = (function() {
    var cached_function = fun1;
         return function() {
    
         // code before override goings here
         a += 5;
    
         cached_function.apply(this, __args__);      // use .apply() to call origial function
    
         // code after override goings here
         };
    }());
    
    fun1();                 // now fun1 is an override function
    t(a, 25);               // a is 25
    

    The test case at:
    https://github.com/unvell/ReoScript/blob/master/TestCase/tests/021_fun.xml

  2. added a commit that references this issue on Jun 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions