From: Benoit M. <mar...@ma...> - 2003-04-04 20:52:24
|
This is a very good idea. See bellow my feedback On Friday, April 4, 2003, at 07:03 AM, Raymond Irving wrote: > Hi, > > > I've been thinking about creating an easier way to > subclass/overwrite methods: > > DynObject.prototype.subclass = function(n,fn){ > if(!this._sbCls) this._sbCls=1; > var om = '_sbMethod'+(this._sbCls++); // old method > > eval('fn='+(fn+'').replace(/\~subclass\(/g,'this.'+om+'(')); > this[om]=this[n]; > this[n]=fn; > }; > > Usage: > > > MyWidget.prototype.subclass('setSize',function(w,h){ > ~subclass(w,h); // call old setSize method; > // some code here > }); > > The above would subclass setSize() with the new > function. The ~subclass() function will allow you to > call the old/orginal setSize() function I would prefer I prefer the syntax super.setSize(w,h). What about adding DynAPIObject.prototype.overwrite = function(sC,n) { var c = this.frame[sC]; var superMethod = c.prototype._superPrototype[n]; var capitalized = n.charAt(0).toUpperCase() + n.substring(1,n.length); var superMethodOverridename = 'super'+ capitalized; if(superMethod && !c.prototype[superMethodOverridename]) { c.prototype[superMethodOverridename] = superMethod; } }; and in the class you want to overwrite: p.setSize = function(width,height) { this.superSetSize (widt h,height); //Then do your stuff } dynapi.overwrite('EventObject','setSize');//which basically does p.superSetSize = mySuperClass.prototype.setSize that way it's in the same fashin as the setPrototype() ? It's "clean" in the sense that it's important to overrides only the implementation defined is in your super class, guaranteeing a correct overriding if different subclasses overrides the same method multiple time. In javascript, it's easy to directly get the implementation of an of your ascendant, and bypass some ! Benoit > > It's similar to: > > MyWidget.prototype._oldSetSize = > DynLayer.prototype.setSize; > MyWidget.prototype.setSize = > function('setSize',function(w,h){ > this.oldSetSize(w,h); // call old setSize method; > // some code here > }); > > > Is the term "subclass" correct or should we term it as > "overwrite"? We should use overwrite, it's the appropriate term in OO terminology when a subclass modify an inherited method. Benoit > > Any comments? > > -- > Raymond Irving > > __________________________________________________ > Do you Yahoo!? > Yahoo! Tax Center - File online, calculators, forms, and more > http://tax.yahoo.com > > > ------------------------------------------------------- > This SF.net email is sponsored by: ValueWeb: > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > No other company gives more support or power for your dedicated server > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > _______________________________________________ > Dynapi-Dev mailing list > Dyn...@li... > http://www.mail-archive.com/dyn...@li.../ > |
From: Raymond I. <xw...@ya...> - 2003-04-04 22:25:32
|
Please see below: --- Benoit Marchant <mar...@ma...> wrote: > I would prefer > I prefer the syntax super.setSize(w,h). > > What about adding > > DynAPIObject.prototype.overwrite = function(sC,n) { > var c = this.frame[sC]; > var superMethod = > c.prototype._superPrototype[n]; > var capitalized = n.charAt(0).toUpperCase() + > n.substring(1,n.length); > var superMethodOverridename = 'super'+ > capitalized; > if(superMethod && > !c.prototype[superMethodOverridename]) { > c.prototype[superMethodOverridename] = > superMethod; > } > }; > > and in the class you want to overwrite: > p.setSize = function(width,height) { > this.superSetSize (widt h,height); > //Then do your stuff > } > dynapi.overwrite('EventObject','setSize');//which > basically does > p.superSetSize = mySuperClass.prototype.setSize > > > that way it's in the same fashin as the > setPrototype() ? > It's "clean" in the sense that it's important to > overrides only the > implementation defined is in your super class, > guaranteeing a correct > overriding if different subclasses overrides the > same method multiple > time. In javascript, it's easy to directly get the > implementation of > an of your ascendant, and bypass some ! > Looks good. One question though... How would this work with multiple overrides? example: function MyClass1(){} var p= dynapi.setPrototype('MyClass1','DynLayer'); dynapi.overwrite('MyClass1','setSize'); p.setSize=function(w,h){ this.superSetSize(w,h); // code here }; function MyClass2(){} var p= dynapi.setPrototype('MyClass2','MyClass1'); dynapi.overwrite('MyClass2','setSize'); p.setSize=function(w,h){ this.superSetSize(w,h); // will the above superSetSize be DynLayer's or MyClass1 setSize function? // If it's MyClass1 then how will the MyClass1 code access DynLayer's setSize()? // code here }; Also, how will it work with multiple extensions? example: // extension library #1 dynapi.overwrite('DynLayer','setSize'); DynLayer.prototype.setSize=function(w,h){ this.superSetSize(w,h); // code here }; // extension library #2 - loaded after extension #1 dynapi.overwrite('DynLayer','setSize'); DynLayer.prototype.setSize=function(w,h){ this.superSetSize(w,h); // code here }; When setSize id called will it execute code in the following order? object -> extension #2 setSize() -> extension #1 setSize() - DynLayer's orginal setSize() > > Is the term "subclass" correct or should we term > it as > > "overwrite"? > We should use overwrite, it's the appropriate term > in OO terminology > when a subclass modify an inherited method. Thanks -- Raymond Irving > > Benoit > > > > > Any comments? > > > > -- > > Raymond Irving > > > > __________________________________________________ > > Do you Yahoo!? > > Yahoo! Tax Center - File online, calculators, > forms, and more > > http://tax.yahoo.com > > > > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: ValueWeb: > > Dedicated Hosting for just $79/mo with 500 GB of > bandwidth! > > No other company gives more support or power for > your dedicated server > > > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > > _______________________________________________ > > Dynapi-Dev mailing list > > Dyn...@li... > > > http://www.mail-archive.com/dyn...@li.../ > > > __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://tax.yahoo.com |
From: Benoit M. <mar...@ma...> - 2003-04-04 23:24:12
|
See my comments bellow On Friday, April 4, 2003, at 02:25 PM, Raymond Irving wrote: > > Please see below: > > --- Benoit Marchant <mar...@ma...> wrote: >> I would prefer >> I prefer the syntax super.setSize(w,h). >> >> What about adding >> >> DynAPIObject.prototype.overwrite = function(sC,n) { >> var c = this.frame[sC]; >> var superMethod = >> c.prototype._superPrototype[n]; >> var capitalized = n.charAt(0).toUpperCase() + >> n.substring(1,n.length); >> var superMethodOverridename = 'super'+ >> capitalized; >> if(superMethod && >> !c.prototype[superMethodOverridename]) { >> c.prototype[superMethodOverridename] = >> superMethod; >> } >> }; >> >> and in the class you want to overwrite: >> p.setSize = function(width,height) { >> this.superSetSize (widt h,height); >> //Then do your stuff >> } >> dynapi.overwrite('EventObject','setSize');//which >> basically does >> p.superSetSize = mySuperClass.prototype.setSize >> >> >> that way it's in the same fashin as the >> setPrototype() ? >> It's "clean" in the sense that it's important to >> overrides only the >> implementation defined is in your super class, >> guaranteeing a correct >> overriding if different subclasses overrides the >> same method multiple >> time. In javascript, it's easy to directly get the >> implementation of >> an of your ascendant, and bypass some ! >> > > Looks good. One question though... How would this > work with multiple overrides? > > example: > > function MyClass1(){} > var p= dynapi.setPrototype('MyClass1','DynLayer'); > dynapi.overwrite('MyClass1','setSize'); > p.setSize=function(w,h){ > this.superSetSize(w,h); > // code here > }; > > function MyClass2(){} > var p= dynapi.setPrototype('MyClass2','MyClass1'); > dynapi.overwrite('MyClass2','setSize'); > p.setSize=function(w,h){ > this.superSetSize(w,h); > // will the above superSetSize be DynLayer's or > MyClass1 setSize function? The above will call MyClass1.prototype.setSize > // If it's MyClass1 then how will the MyClass1 code > access DynLayer's setSize()? Because the overwrite method creates MyClass1.prototype.superSetSize = DynLayer .prototype.superSetSize > > // code here > }; > > Also, how will it work with multiple extensions? > It doesn't ! There's the exact same problem with categories in Objective-C. If you define the same method (even for new method that didn't exists) on a class in 2 different source files, depending on the loading mechanism, either the first or the last would win. There's no way to determine an order between the multiple definitions, and even if there were an order, at best you want to control which one wins, but defining any combining of the different implementations looks risky to me. So bottom line we should decide which one wins, and stick with that. In the implementation I did, the first one win as I check if the superSetSize exists, and if it does, then subsequent use overwrite would have no effects. If we don't check, then the last call win. I guess, if you provide a extension it would be called after and you would expect it to win. This overwrite use is meant for overriding method in subclasses. Now if you want to re define setSize on DynLayer with your custom implementation, and then have all subclasses that inherit from DynLayer and override setSize to use your new implementation, you really have to assign DynLayer.prototype.setSize = myNewSetSize And all should be as expected. But that usage could be described as a "hack". The same can be done in the Objective-C run-time, but I've only done it once. This usage should be pretty rare, and therefore you can do it manually I think. Make sense ? Benoit > example: > > // extension library #1 > dynapi.overwrite('DynLayer','setSize'); > DynLayer.prototype.setSize=function(w,h){ > this.superSetSize(w,h); > // code here > }; > > // extension library #2 - loaded after extension #1 > dynapi.overwrite('DynLayer','setSize'); > DynLayer.prototype.setSize=function(w,h){ > this.superSetSize(w,h); > // code here > }; > > When setSize id called will it execute code in the > following order? > > object -> extension #2 setSize() -> extension #1 > setSize() - DynLayer's orginal setSize() > >>> Is the term "subclass" correct or should we term >> it as >>> "overwrite"? >> We should use overwrite, it's the appropriate term >> in OO terminology >> when a subclass modify an inherited method. > > Thanks > > -- > Raymond Irving > >> >> Benoit >> >>> >>> Any comments? >>> >>> -- >>> Raymond Irving >>> >>> __________________________________________________ >>> Do you Yahoo!? >>> Yahoo! Tax Center - File online, calculators, >> forms, and more >>> http://tax.yahoo.com >>> >>> >>> >> > ------------------------------------------------------- >>> This SF.net email is sponsored by: ValueWeb: >>> Dedicated Hosting for just $79/mo with 500 GB of >> bandwidth! >>> No other company gives more support or power for >> your dedicated server >>> >> > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ >>> _______________________________________________ >>> Dynapi-Dev mailing list >>> Dyn...@li... >>> >> > http://www.mail-archive.com/dyn...@li.../ >>> >> > > > __________________________________________________ > Do you Yahoo!? > Yahoo! Tax Center - File online, calculators, forms, and more > http://tax.yahoo.com > > > ------------------------------------------------------- > This SF.net email is sponsored by: ValueWeb: > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > No other company gives more support or power for your dedicated server > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > _______________________________________________ > Dynapi-Dev mailing list > Dyn...@li... > http://www.mail-archive.com/dyn...@li.../ > |
From: Raymond I. <xw...@ya...> - 2003-04-06 03:31:13
|
--- Benoit Marchant <mar...@ma...> wrote: > > function MyClass2(){} > > var p= dynapi.setPrototype('MyClass2','MyClass1'); > > dynapi.overwrite('MyClass2','setSize'); > > p.setSize=function(w,h){ > > this.superSetSize(w,h); > > // will the above superSetSize be DynLayer's or > > MyClass1 setSize function? > The above will call MyClass1.prototype.setSize > > // If it's MyClass1 then how will the MyClass1 > code > > access DynLayer's setSize()? > Because the overwrite method creates > MyClass1.prototype.superSetSize = > DynLayer .prototype.superSetSize I'll have to run some more test on this one. > > Also, how will it work with multiple extensions? > > > It doesn't ! There's the exact same problem with > categories in > Objective-C. If you define the same method (even for > new method that > didn't exists) on a class in 2 different source > files, depending on the > loading mechanism, either the first or the last > would win. > There's no way to determine an order between the > multiple definitions, > and even if there were an order, at best you want to > control which one > wins, but defining any combining of the different > implementations looks > risky to me. > So bottom line we should decide which one wins, and > stick with that. > In the implementation I did, the first one win as I > check if the > superSetSize exists, and if it does, then subsequent > use overwrite > would have no effects. > If we don't check, then the last call win. I guess, > if you provide a > extension it would be called after and you would > expect it to win. > > This overwrite use is meant for overriding method in > subclasses. Now if > you want to re define setSize on DynLayer with your > custom > implementation, and then have all subclasses that > inherit from DynLayer > and override setSize to use your new implementation, > you really have to > assign DynLayer.prototype.setSize = myNewSetSize > > And all should be as expected. But that usage could > be described as a > "hack". The same can be done in the Objective-C > run-time, but I've only > done it once. This usage should be pretty rare, and > therefore you can > do it manually I think. > > Make sense ? Current we are able to support multiple extension that can overwrite a class function multiple times without having to worry about who comes fies or last. If we start allowing users to use overwrite() then they will just use it and expect it to work the same way they can currently do overwrites. We'll have to come up with a solution that supports multiple overwrites of the same class function. -- Raymond Irving > Benoit > __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://tax.yahoo.com |