View Single Post
  #1  
Old 03-02-2010, 02:36 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Lexically Binding a Function

I was playing with function closures in GS2, trying to make them do something useful, and after hacking away at the callstack for awhile, I finally managed to do this.

PHP Code:
join("utility_bind");
//#CLIENTSIDE
function foobar(temp.atemp.b) {
  
temp.5;
  
temp.baz = function (temp.x) {
    
temp.scope this.getScope();
    return 
temp.scope.temp.scope.temp.scope.temp.x;
  };
  
this.bind(temp.baz);
  return 
temp.baz;
}
function 
onCreated() {
  
temp.this.foobar(510);
  echo(
temp.f(5)); // echos 25

(Beware that copying this directly will add a space after every line, screwing up //#CLIENTSIDE)

Basically, you can bind the current state of the outer function (foobar) to any function you pass (temp.baz), and it will be immutable until the bound function executes this.getScope() and stores it.


The magic is attached.


getCallStack() only works clientside in v6, so you'll need that to see this in action.
Attached Files
File Type: txt utility_bind.txt (246 Bytes, 360 views)

Last edited by WhiteDragon; 03-02-2010 at 05:00 AM..
Reply With Quote