Scoping / Encapsulation / Closures Behaviors Oh My!

21 views
Skip to first unread message

JoshuaIRL

unread,
Oct 3, 2012, 8:51:40 AM10/3/12
to ra...@googlegroups.com
Hello AGAIN, :D

I have some concerns with components that implement other components potentially losing site of the "this" scope.
Maybe it's just something with my code but the best way I can explain it is with pseudo-code for now.  That's all I have time for.
It could be 'just me' but I figured I would share in case I'm doing something wrong.

Let's say we have 1 cfc, we'll call it "utilities.cfc" which has the following code:
component {
public any function init() {
return this;
};
public any function mapper(arr,func) {
      func(do,something,now);
};

public any function doer(arr,func) {
     func(do,something,now);
};

public any function donter(arr,func) {
       func(do,something,now);
};
}


THEN, we have another CFC that uses this utilities.cfc, we'll call it 'worker.cfc' which has code like such...
component {
     public any function init() {
           variables.util = new utilities();
     }
    

     public any function fooer(arr) {
            util.mapper(arr,this.doerFunc);
     }

     public any function doerFunc(this,that,andthis) {
           writeDump(var=this,abort=true); //'this' now represents the "utilities.cfc" because I passed this function to the util.mapper.
     }
}

Is this normal behavior? am I just stupid? :D
I mean, I assume the reason it's acting this way is because 'doerFunc' is passed as a function expression to the utilities.cfc function which now executes it under it's own scope but is this intentional?  Should this work this way?

I'll try to get a real test case up as soon as I can.

Please advise.

Sincerely,
Joshua F. Rountree

Jochem van Dieten

unread,
Oct 3, 2012, 2:17:25 PM10/3/12
to ra...@googlegroups.com
On Wed, Oct 3, 2012 at 2:51 PM, JoshuaIRL wrote:
> I have some concerns with components that implement other components
> potentially losing site of the "this" scope.

> public any function doerFunc(this,that,andthis) {
> writeDump(var=this,abort=true); //'this' now represents the
> "utilities.cfc" because I passed this function to the util.mapper.
> }

If you name a function argument "this" and refer to it without
explicitly scoping it as "arguments.this" your code deserves to loose
track of scopes.

Jochem


--
Jochem van Dieten
http://jochem.vandieten.net/

JoshuaIRL

unread,
Oct 3, 2012, 2:20:34 PM10/3/12
to ra...@googlegroups.com
Jochem,

Oops, I didn't realize I named my argument that in my pseudo code...
Review my question in context with this:

Let's say we have 1 cfc, we'll call it "utilities.cfc" which has the following code:
component {
public any function init() {
return this;
};
public any function mapper(arr,func) {
      func(do,something,now);
};

public any function doer(arr,func) {
     func(do,something,now);
};

public any function donter(arr,func) {
       func(do,something,now);
};
}


THEN, we have another CFC that uses this utilities.cfc, we'll call it 'worker.cfc' which has code like such...
component {
     public any function init() {
           variables.util = new utilities();
     }
    

     public any function fooer(arr) {
            util.mapper(arr,this.doerFunc)
;
     }

     public any function doerFunc(something,somethingelse,whatever) {

JoshuaIRL

unread,
Oct 3, 2012, 2:26:44 PM10/3/12
to ra...@googlegroups.com
Also, here is a library that is affected by this issue, although, it is not specific to this library, any CFC that you do this with will happen.
https://github.com/russplaysguitar/UnderscoreCF/issues/10

Michael Offner

unread,
Oct 3, 2012, 6:54:22 PM10/3/12
to ra...@googlegroups.com
The this scope is special because it is not really a scope, in acf you can even do things like this:
This="";
After that you can no longer use the this scope?! Railo simply ignores when you try to overwrite the this scope, the same way like overwriting any scope out site a function is not possible.
Then in acf you can also something like
function test(string url) {...
In acf you can no longer access the URL scope inside this function, in Railo you can because the keyword URL overrules the argument key "URL".
 
/micha


--
/micha

Michael Offner CTO Railo Technologies GmbH

JoshuaIRL

unread,
Oct 3, 2012, 8:13:56 PM10/3/12
to ra...@googlegroups.com
Here is a testable git repo of the outlined issue.
https://github.com/joshuairl/railo4-test-this-scope
If you run index.cfm within Railo 4 it should show exactly the problem.



On Wednesday, October 3, 2012 8:51:40 AM UTC-4, JoshuaIRL wrote:
Reply all
Reply to author
Forward
0 new messages