JSFiddle

  • Inheritance with operator new

    The traditional (ES3 and earlier) approach to inheritance in JavaScript.

    var Person = function (name, birthdate) {
        this.name = name;
        this.birthdate = birthdate;
    };
    
    /*
     * Returns the age, in years, of the person.  Assumes ...
  • Shapes using operator new

    Some JavaScript shape objects implemented with operator new, with unit tests, of course

    /*
     * A circle datatype.
     *
     * Synopsis:
     * let c = new Circle(10);
     * c.radius ==> 10
     * c.area() ==> 314.1592653589793
     * c.perimeter() ==> 62.83185307179586 ...
  • Shapes with Object.create

    Some JavaScript shape objects implemented with Object.create, with unit tests, of course

    /*
     * A circle datatype.
     *
     * Synopsis:
     * let c = Circle.create(10);
     * c.radius ==> 10
     * c.area() ==> 314.1592653589793
     * c.perimiter() ==> 62.83185307179586 ...
  • Shallow or Deep Binding? #4 is the latest revision

    A JavaScript implementation of Michael L. Scott's shallow/deep binding tester

    /*
     * Translation into JavaScript of a code fragment in Michael
     * L. Scott's Pragmatics of Programming Languages used to
     * illustrate shallow ...
  • JavaScript median of three

    Three ways to compute the median-of-three in JavaScript

    /*
     * An illustration of several ways to implement the median of three
     * in JavaScript.
     */
    
    let medians = [
        
      /*
       * Returns the median of its ...
  • Nested Canvas Squares #23 is the latest revision

    Small hack to show 30 nested squares in JavaScript using Canvas

    const canvas = document.getElementById("squares");
    const ctx = canvas.getContext("2d");
    const centerX = canvas.width / 2;
    const centerY = canvas.height / 2 ...