[mochikit] Re: Class inheritance
other.prototype = Helper doesn't help. Since bindMethods has made sure that 'this' always refers to the class that it was invoked in, the methods loose scope when you use them. Troels, the problem with using prototypical inheritance is that you end up losing scope in, for example, AJAX request responses or DOM event handlers. --~--~-~--~~~---~--~~ 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: Class inheritance
On Wed, Oct 8, 2008 at 12:56 AM, Akari no ryu [EMAIL PROTECTED] wrote: Troels, the problem with using prototypical inheritance is that you end up losing scope in, for example, AJAX request responses or DOM event handlers. If you want to keep scope in an event handler, you can delegate to it, using a closure: connect( 'myID', 'onclick', function() { foo.bar(); }); Or, if you prefer, you can use the MochiKit-provided `bind` function, to do the same: connect('myID', 'onclick', bind('bar', foo)); -- troels --~--~-~--~~~---~--~~ 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: Class inheritance
Perhaps the problem is that you've already used bindMethods on the Helper class before subclassing? That might break the inheritance depending on how the code looks, since it reassigns the this object pointer to another instance. Also, you must use the new keyword, or it won't work: other.prototype = new Helper(); Keeping the scope for DOM events is one of the points with MochiKit.Signal: connect(elem, onclick, obj, method); Or one could use bind when setting each callback. Using bindMethods is also ok, within some constraints. /Per On Wed, Oct 8, 2008 at 12:56 AM, Akari no ryu [EMAIL PROTECTED] wrote: other.prototype = Helper doesn't help. Since bindMethods has made sure that 'this' always refers to the class that it was invoked in, the methods loose scope when you use them. Troels, the problem with using prototypical inheritance is that you end up losing scope in, for example, AJAX request responses or DOM event handlers. --~--~-~--~~~---~--~~ 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: Class inheritance
A far more effective method for this I think is just to do protype inheritance the way javascript is designed. other.prototype = Helper; no looping over accessors and trying to figure out if you already have this method or accessor in your object. I had never really thought of MochiKit as an OO helper library. The joose project is probably closer to what you are looking for in a class helper: http://code.google.com/p/joose-js/ MochiKit I think has different goals. Disclaimer: I am an occasional contributer to the joose project. On Mon, Oct 6, 2008 at 10:40 AM, Akari no ryu [EMAIL PROTECTED]wrote: Ok, I've searched through the archives for posts referencing the word inheritance but most of them are just referring to prototype and the lack of scope that JS gives you if you don't bindMethods(this) I'm asking about actual OO inheritance in this post, wherein a class inherits methods from another class. I've also tried google but that was utterly useless. Gave me suggestions for code which doesn't work when you're using MochiKit, because of the bindMethods function. I've added a bequeathTo method to my helper class which does the following Helper.prototype.bequeathTo = function(other) { for(var i in this) { other[i] = MochiKit.base.bind(this[i], other) } } That works. It's been tested and implemented in two classes that inheret methods from it. I'll upload my code for linkage later. I don't know if all y'all want to implement this but I though I'd post just in case. --~--~-~--~~~---~--~~ 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: Class inheritance
I never used `bindMethods`. I can see why a Pythonista might find it intuitive, but quite frankly, it's reveals a fundamental misunderstanding of the language being used. Javascript does not have classes, and trying to emulate them is going to be a very leaky abstraction. You would be much better off using the features that the language supports (prototypical inheritance). Maybe the documentation should include a note about this, under `bindMethods`? -- troels On Mon, Oct 6, 2008 at 5:40 PM, Akari no ryu [EMAIL PROTECTED] wrote: Ok, I've searched through the archives for posts referencing the word inheritance but most of them are just referring to prototype and the lack of scope that JS gives you if you don't bindMethods(this) I'm asking about actual OO inheritance in this post, wherein a class inherits methods from another class. I've also tried google but that was utterly useless. Gave me suggestions for code which doesn't work when you're using MochiKit, because of the bindMethods function. I've added a bequeathTo method to my helper class which does the following Helper.prototype.bequeathTo = function(other) { for(var i in this) { other[i] = MochiKit.base.bind(this[i], other) } } That works. It's been tested and implemented in two classes that inheret methods from it. I'll upload my code for linkage later. I don't know if all y'all want to implement this but I though I'd post just in case. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---