From 7bbe44adc311162c26b3a27e220f1beb07c72eb5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 20 Mar 2011 23:18:46 -0400 Subject: [PATCH] Added section in manual for overriding methods --- doc/classes.texi | 55 +++++++++++++++++++++++++++++++++++++ doc/img/inheritance-ex.dia | Bin 1514 -> 1566 bytes 2 files changed, 55 insertions(+) diff --git a/doc/classes.texi b/doc/classes.texi index fafde60..b16bbc8 100644 --- a/doc/classes.texi +++ b/doc/classes.texi @@ -392,6 +392,7 @@ conciseness. @menu * Understanding Member Inheritance:: How to work with inherited members +* Overriding Methods:: Overriding inherited methods @end menu @node Understanding Member Inheritance @@ -456,6 +457,60 @@ inherited from @var{Dog}. If we actually run the example, you will notice that the dog does indeed bark, showing that we are able to call our parent's method even though we did not define it ourselves. +@node Overriding Methods +@subsection Overriding Methods +When a method is inherited, you have the option of either keeping the parent's +implementation or overriding it to provide your own. When you override a method, +you replace whatever functionality was defined by the parent. Using this concept +is how we made our lazy dog lazy and our two-legged dog walk on two legs in +@ref{f:inheritance}. + +After overriding a method, you may still want to invoke the parent's method. +This allows you to augment the functionality rather than replacing it entirely. +ease.js provides a magic @code{__super()} method to do this. This method is +defined only for overriding methods and calls the parent method that was +overridden. + +In order to demonstrate this, let's add an additional subtype to our hierarchy: +@var{AngryDog}. @var{AngryDog} will be a subtype of @var{LazyDog}. Not only is +this dog lazy, but he's rather moody. + +@float Figure, f:super-method +@verbatim + var AngryDog = Class( 'AngryDog' ).extend( LazyDog, + { + 'public poke': function() + { + // augment the parent method + console.log( 'Grrrrrr...' ); + + // call the overridden method + this.__super(); + } + } ); + + // poke a new AngryDog instance + AngryDog().poke(); + + // Output: + // Grrrrrr... + // Woof! +@end verbatim +@caption{Using @code{__super()} method} +@end float + +If you remember from @ref{f:using-inherited-members}, we added a @code{poke()} +method to @var{LazyDog}. In @ref{f:super-method} above, we are overriding this +method so that @var{AngryDog} growls when you poke him. However, we still want +to invoke @var{LazyDog}'s default behavior when he's poked, so we also call the +@code{__super()} method. This will also make @var{AngryDog} bark like +@var{LazyDog}. + +It is important to note that @code{__super()} must be invoked like any other +method. That is, if the overridden method requires arguments, you must pass them +to @code{__super()}. This allows you to modify the argument list before it is +sent to the overridden method. + @node Member Visibility @section Member Visibility diff --git a/doc/img/inheritance-ex.dia b/doc/img/inheritance-ex.dia index 10937adc2dd203e99337fd1106687fae40bbdd3b..c1083afbc611a19f95d71bd18f26dd5d44e760b5 100644 GIT binary patch literal 1566 zcmV+(2I2W1iwFP!000021MOT(bDKyIzWY~D_-c=M3Mq-4s$^?-58lcpIjcr6LbDzi zb{H%xhyCq4AoKu)BtTH>)KHb!LQQ|&&2-cAwTn-m-vXk%A{JvB&Qu*}s)9n7dN}lF z>W}ZgKTg!oPj{a@3_pl}K7)ZGt_b7w(@b4)9(@>%*6TGOn;3G+0Ko}}(db`D2pkEa zk@|F}D8~vs$YFXeKMOf$c#&|VgfKuebphQMpV1`r)U8x5=TbtM@(RgJyAIj#L8-eh{jlm@J&<^snL`Ee0bpYER#=ZM{J z&B{+6^qn%c7KjIs`8cd%iTIFhFbrUu)AShY6Wi1b$K1uGIox7cI5I388x{`7^N2Fe zAm&xb1*HVRaEr=Wg4*N6E+k@!opVcXuYHL*rx)yBLK0u&!1nY$D~88};S8lU4A;SeiW1G7C46Ldg%c+5~f8e=e?+7A{moR*M?pkVG&7FIijTY8qkWCf+_-L9CJ zK%ZLEqhrzmYlX1C;`Lx6WLn`c+JTkYr$Y1e8}(0iPEGX3qWwZNUA}cFogqYa1}Nmc z>4yt(t;o+Zz`5scqYx!g#84d1_g}=lLs8LLLoUikZ&14j*p*Hd`>r-XSGv?4C|_^A zMQ+Vt)W0_w3S5fxoVzPnDW3b%X=&~B=TVxAei+RO3jJ#7JTx*+Hw+Gazu0!)^oC?E zTqyn;(2Mn0(>s6_8>*UJx7PB`EHNRM5sH*SGX##_c)vb`RO}$hd`YWyKRu@`VSveI zrUo>maRgn_Va0rtwp*fG+N$_U6NV9Mhrf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