<!-- Examples 13.78 through 13.80 (Figure 13.27)

    This file should work in any JavaScript-enabled browser, whether
    loaded from an http:// URI or a file:// URI.
-->

<HTML>
<HEAD>
<TITLE>Object Orientation</TITLE>
<SCRIPT type="text/javascript">

function Integer(n) {
    this.val = n || 0;      // use 0 if n is missing (undefined)
}

function Integer_set(n) {
    this.val = n;
}

function Integer_get() {
    return this.val;
}

Integer.prototype.set = Integer_set;
Integer.prototype.get = Integer_get;

function Tally(n) {
    this.base(n);                   // call to base constructor
}
function Tally_inc() {
    this.val++;
}
Tally.prototype = new Integer;      // inherit methods
Tally.prototype.base = Integer;     // make base constructor available
Tally.prototype.inc = Tally_inc;    // new method

function foo() {
    x = document.getElementById('line1');
    y = document.getElementById('line2');

    c2 = new Integer(3);
    c3 = new Integer;

    document.write(c2.get() + "&nbsp;&nbsp;&nbsp;" + c3.get() + "<BR>");
    c2.set(4);  c3.set(5);
    document.write(c2.get() + "&nbsp;&nbsp;&nbsp;" + c3.get() + "<BR>");

    c2.set = new Function("n", "this.val = n * n;");
        // anonymous function constructor
    c2.set(3);  c3.set(4);      // these call different methods!
    document.write(c2.get() + "&nbsp;&nbsp;&nbsp;" + c3.get() + "<BR>");

    t1 = new Tally(3);
    t1.inc();  t1.inc();
    document.write(t1.get() + "<BR>");
}

</SCRIPT>
</HEAD>
<BODY>
<P><SPAN id="line1"></SPAN><BR>
<P><SPAN id="line2"></SPAN>
<P><SCRIPT type="text/javascript"> foo(); </SCRIPT>
</BODY>
</HTML>
