/**
* com/davidflanagan/Complex.js: a class representing complex numbers
*
* This module defines the constructor function com.davidflanagan.Complex( )
* This module requires the com/davidflanagan/Class.js module
**/
// First, check for the Class module
var com; // Declare global symbol before testing for its presence
if (!com || !com.davidflanagan || !com.davidflanagan.Class)
throw new Error("com/davidflanagan/Class.js has not been loaded");
// We know from this test that the com.davidflanagan namespace
// exists, so we don't have to create it here. We'll just define
// our Complex class within it
com.davidflanagan.Complex = com.davidflanagan.Class.define({
name: "Complex",
construct: function(x,y) { this.x = x; this.y = y; },
methods: {
add: function(c) {
return new com.davidflanagan.Complex(this.x + c.x,
this.y + c.y);
}
},
});
var newVar = com.davidflanagan.Complex.add(5);
alert(newVar);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.