View previous topic :: View next topic |
Author |
Message |
maustin
Joined: 15 Jun 2009 Posts: 10
|
Posted: Sun Oct 04, 2009 12:45 pm Post subject: "this" and anonymous functions |
|
|
Any thoughts about the "this" issue relating to anonymous functions?
function foo() {
this.field = 'name'
arr.sort(\a, b -> a[field] <=> b[field])
}
In Javascript there's a workaround like "self = this" and then use self, but it's kind of inelegant. In Smalltalk and Ruby, self is the same as the calling function and it tremendously useful.
I haven't been around in a while, but minid is still my fav language! See ya Jarret.. |
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Sun Oct 04, 2009 3:24 pm Post subject: |
|
|
Hey! Haven't seen you in a while. But it means a lot when you say it's your favorite language
Unfortunately you have to use the same workaround in MiniD as in Javascript. Alternatively you can use bindContext, but it's still not automatic. The issue is that when the nested function is called back from whatever you call it from, the 'this' from the owning function has been lost. I'm pretty sure smalltalk and ruby manage this by having two types (?) of functions, one with a bound 'this' and one without, but I'm not entirely clear on how they're implemented, so I could be wrong.
It's something I'll keep in mind in the future though. I actually had a friend complain to me about this same issue when he was doing a lot of JS. |
|
Back to top |
|
|
maustin
Joined: 15 Jun 2009 Posts: 10
|
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Mon Oct 05, 2009 12:51 am Post subject: |
|
|
Sweet. Well with any luck, by the time you get back into it, I'll be motivated to work on the GC. Faster GC means even better realtime performance, wee! |
|
Back to top |
|
|
maustin
Joined: 15 Jun 2009 Posts: 10
|
Posted: Mon Oct 05, 2009 4:32 pm Post subject: |
|
|
JarrettBillingsley wrote: | Hey! Haven't seen you in a while. But it means a lot when you say it's your favorite language
Unfortunately you have to use the same workaround in MiniD as in Javascript. Alternatively you can use bindContext, but it's still not automatic. The issue is that when the nested function is called back from whatever you call it from, the 'this' from the owning function has been lost. I'm pretty sure smalltalk and ruby manage this by having two types (?) of functions, one with a bound 'this' and one without, but I'm not entirely clear on how they're implemented, so I could be wrong.
It's something I'll keep in mind in the future though. I actually had a friend complain to me about this same issue when he was doing a lot of JS. |
In Smalltalk, a block acts just as though it was executed in the containing method - you can also return from the method. Ruby has both versions, which I think could be confusing to beginners.
I remember you talking about the older version of minid that supported this, but was prone to security risks? Can you refresh my memory?
Mike |
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Tue Oct 06, 2009 4:59 am Post subject: |
|
|
Quote: | In Smalltalk, a block acts just as though it was executed in the containing method - you can also return from the method. Ruby has both versions, which I think could be confusing to beginners. |
Right right, and they both do it with "magic" IIRC Ruby has some kind of block stack that it uses to keep track of the owning function, so it knows how far to unwind the call stack when you do a return in a block.
And yes, you're right - I did play around with the concept in MiniD. It wasn't prone to security risks AFAIR but it was just.. icky. The language wasn't designed to have it from the start, so it felt really tacked-on. Also, not having used languages like Smalltalk and Ruby extensively, I wasn't terribly committed to the idea It's something I'll reconsider later though. |
|
Back to top |
|
|
|