1 /*! This file exists only for testing a Minify installation. It's content is not used.
3 * http://example.org/min/f=min/quick-test.js
6 /* Finds the lowest common multiple of two numbers */
7 function LCMCalculator(x, y) { // constructor function
8 var checkInt = function (x) { // inner function
10 throw new TypeError(x + " is not an integer"); // throw an exception
15 // ^ semicolons are optional
18 // The prototype of object instances created by a constructor is
19 // that constructor's "prototype" property.
20 LCMCalculator.prototype = { // object literal
21 constructor: LCMCalculator, // when reassigning a prototype, set the constructor property appropriately
22 gcd: function () { // method that calculates the greatest common divisor
23 // Euclidean algorithm:
24 var a = Math.abs(this.a), b = Math.abs(this.b), t;
36 // Only need to calculate GCD once, so "redefine" this method.
37 // (Actually not redefinition - it's defined on the instance itself,
38 // so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.)
39 // Also, 'gcd' === "gcd", this['gcd'] === this.gcd
40 this['gcd'] = function () {
45 // Object property names can be specified by strings delimited by double (") or single (') quotes.
47 // Variable names don't collide with object properties, e.g. |lcm| is not |this.lcm|.
48 // not using |this.a * this.b| to avoid FP precision issues
49 var lcm = this.a / this.gcd() * this.b;
50 // Only need to calculate lcm once, so "redefine" this method.
51 this.lcm = function () {
56 toString: function () {
57 return "LCMCalculator: a = " + this.a + ", b = " + this.b;
61 //define generic output function; this implementation only works for web browsers
63 document.write(x + "<br>");
66 // Note: Array's map() and forEach() are defined in JavaScript 1.6.
67 // They are used here to demonstrate JavaScript's inherent functional nature.
68 [[25, 55], [21, 56], [22, 58], [28, 56]].map(function (pair) { // array literal + mapping function
69 return new LCMCalculator(pair[0], pair[1]);
70 }).sort(function (a, b) { // sort with this comparative function
71 return a.lcm() - b.lcm();
72 }).forEach(function (obj) {
73 output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());