[mochikit] callLater and this
Is it intentional that the callLater() function causes the 'this' reference to get scrapped? Say I run the following. It should call the __repr__ function for my object that I created but it I suppose the function gets moved to another object during the callLater call. See http://www.ianlewis.org/dump/test.html for a running example. test = new Object(); test.__repr__ = function() { return This is my repr.; } test.myfunc = function() { logDebug(repr(this)); } test.init = function() { createLoggingPane(true); callLater(3.0, test.myfunc); } addLoadEvent(test.init); -- Ian Lewis [EMAIL PROTECTED] http://www.ianlewis.org --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: callLater and this
On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote: Is it intentional that the callLater() function causes the 'this' reference to get scrapped? Has nothing to do with MochiKit, JavaScript doesn't have bound methods like Python does. The binding of this is done by the method call syntax. It would be impossible to preserve it unless the function was wrapped. See MochiKit.Base.{bind,method,bindMethods} for the typical workarounds, also: http://bob.pythonmac.org/archives/2005/07/06/this-sucks-in-javascript/ -bob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: callLater and this
On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote: Thanks bob, I guess it's been about 8 years. Good to talk to you again. High school feels a lot longer ago than that :) Tricky. I suppose you would run into this pretty much any time you pass around foo.bar when bar bound (or not) to the object foo as you wouldn't be able to do the call with this syntax. If your blog post is right then it looks like the only way to do call with this explicitly calling the function via the object in code. Welcome to JavaScript. That's pretty much what bind does, but Function.prototype.call and Function.prototype.apply let you jigger this for a given call. foo.bar(a, b) is the same as foo.bar.apply(foo, [a, b]) or foo.bar.call(foo, a, b). Anyway, changing the test code to test.init = function() { createLoggingPane(true); callLater(3.0, bind(test.myfunc, test)); } works. It's cute how bind returns a function that calls your function but goes out and finds the this for you first. Pretty useful hack. Well you give it 'this' as the second argument, it doesn't do all that much magic. bind(test.myfunc, test) is equivalent to function () { return test.myfunc.apply(test, arguments); } -bob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: callLater and this
Thanks bob, I guess it's been about 8 years. Good to talk to you again. Tricky. I suppose you would run into this pretty much any time you pass around foo.bar when bar bound (or not) to the object foo as you wouldn't be able to do the call with this syntax. If your blog post is right then it looks like the only way to do call with this explicitly calling the function via the object in code. Anyway, changing the test code to test.init = function() { createLoggingPane(true); callLater(3.0, bind(test.myfunc, test)); } works. It's cute how bind returns a function that calls your function but goes out and finds the this for you first. Pretty useful hack. Ian 2007/12/21, Bob Ippolito [EMAIL PROTECTED]: On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote: Is it intentional that the callLater() function causes the 'this' reference to get scrapped? Has nothing to do with MochiKit, JavaScript doesn't have bound methods like Python does. The binding of this is done by the method call syntax. It would be impossible to preserve it unless the function was wrapped. See MochiKit.Base.{bind,method,bindMethods} for the typical workarounds, also: http://bob.pythonmac.org/archives/2005/07/06/this-sucks-in-javascript/ -bob -- Ian Lewis [EMAIL PROTECTED] http://www.ianlewis.org -- ルイス イアン ソートス株式会社 〒101-0047 東京都千代田区内神田1丁目3番1号高砂ビル9階 TEL : 03-5577-0800 FAX : 03-5259-0880 E-Mail: [EMAIL PROTECTED] URL : http://www.thought-s.co.jp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: callLater and this
2007/12/21, Bob Ippolito [EMAIL PROTECTED]: On 12/20/07, Ian Lewis [EMAIL PROTECTED] wrote: Thanks bob, I guess it's been about 8 years. Good to talk to you again. High school feels a lot longer ago than that :) Indeed. Welcome to JavaScript. I feel a my eyes twitching when I go to sleep. That's pretty much what bind does, but Function.prototype.call and Function.prototype.apply let you jigger this for a given call. foo.bar(a, b) is the same as foo.bar.apply(foo, [a, b]) or foo.bar.call(foo, a, b). Thank goodness it's there. works. It's cute how bind returns a function that calls your function but goes out and finds the this for you first. Pretty useful hack. Well you give it 'this' as the second argument, it doesn't do all that much magic. Only if you're kind enough to give it. I just happen to be a nice guy. Anywho, thanks for the insight. -- Ian Lewis [EMAIL PROTECTED] http://www.ianlewis.org --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---