// This example is from the book _JavaScript: The Definitive Guide_.    
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// This example is provided WITHOUT WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for any purpose.        

function Complex(x,y) {
    this.x = x;   // real part of complex number
    this.y = y;   // imaginary part of complex number.
}

// force the prototype object to be created.
new Complex(0,0); 

// define some methods
Complex.prototype.valueOf = new Function("return this.x");
Complex.prototype.toString = new Function("return '{'+this.x+','+this.y+'}'");

// create new complex number object
c = new Complex(4,1);  

// Now rely on the valueOf() operator to treat it like a real number
// Note that this wouldn't work with the + operator--that would convert
// the object to a string and do string concatenation.
x = c * 2;          // x = 8
x = Math.sqrt(c);   // x = 2
