HTML and CSS Reference
In-Depth Information
diameter: diameter,
area: area,
circumference: circumference
Because circle is no longer a constructor, its name is no longer capitalized.
To use this new function we omit the new keyword as seen in Listing 7.45.
Listing 7.45 Using the functional inheritance pattern
"test should create circle object with function":
function () {
var circ = circle(6);
assertEquals(6, circ.radius());
assertEquals(12, circ.radius());
assertEquals(24, circ.diameter());
Crockford calls an object like the ones created by circle durable [6]. When an
object is durable, its state is properly encapsulated, and it cannot be compromised by
outside tampering. This is achieved by keeping state in free variables inside closures,
and by never referring to the object's public interface from inside the object. Recall
how we defined all the functions as inner private functions first, and then assigned
them to properties? By always referring to the object's capability in terms of these
inner functions, offending code cannot compromise our object by, e.g., injecting its
own methods in place of our public methods. Extending Objects
How can we achieve inheritance using this model? Rather than returning a new
object with the public properties, we create the object we want to extend, add
methods to it, and return it. You might recognize this design as the decorator pat-
tern, and it is. The object we want to extend can be created in any way—through
a constructor, from another object producing function, or even from the argu-
ments provided to the function. Listing 7.46 shows an example using spheres and
Search WWH ::

Custom Search