20,741
edits
m (→Variables) |
|||
Line 1,465: | Line 1,465: | ||
=== More on methods === | === More on methods: Chaining === | ||
Methods are function members of a class hash. They can access other class members via the <tt>me</tt> variable, which is a reference to the class hash. For this reason, a method returning <tt>me</tt> can be used like the class itself, and one can apply further methods to the return value (this is usually called "method chaining"): | Methods are function members of a class hash. They can access other class members via the <tt>me</tt> variable, which is a reference to the class hash. For this reason, a method returning <tt>me</tt> can be used like the class itself, and one can apply further methods to the return value (this is usually called "method chaining"): | ||
Line 1,490: | Line 1,490: | ||
triangle.translate(-9, -4).scale(5).rotate(33).translate(9, 4); # concatenated methods thanks to "me" | triangle.translate(-9, -4).scale(5).rotate(33).translate(9, 4); # concatenated methods thanks to "me" | ||
=== More on methods: Methods as Listener Callbacks === | |||
<tt>me</tt>, however, is only known in the scope of the class. If a method is to be called as a listener callback or a timer function, <tt>me</tt> has to get wrapped in a function, so that it's stored in the function closure. | <tt>me</tt>, however, is only known in the scope of the class. If a method is to be called as a listener callback or a timer function, <tt>me</tt> has to get wrapped in a function, so that it's stored in the function closure. |