title: Override javascript date constructor
body: <p>I have my own jquery plugin for a date picker control that is rendered with a MVC HtmlHelper extension method. Its for dates in the format dd/mm/yyyy (Australian date).</p>
  
  <p>To make it easier construct a javascript date object from the input <code>&lt;input type=text .../&gt;</code>, I overrode the constructor as follows:</p>
  
  <pre><code>// Save the default constructor 
  var _DefaultDateConstructor = Date.prototype.constructor;
  // Constructor
  Date = function () {
    var date = new _DefaultDateConstructor();
    if (arguments === undefined || arguments.length === 0) {
      // Ensure an invalid date is returned
      date.setDate(Number.NaN);
      return date;
    }
    // Get the arguments
    var args = arguments[0].toString().split(/[\/ ]/);
    // Set date properties
    date.setMilliseconds(0);
    date.setSeconds(0);
    date.setMinutes(0);
    date.setHours(0);
    date.setDate(parseInt(args[0], 10));
    date.setMonth(parseInt(args[1], 10) - 1);
    date.setFullYear(parseInt(args[2], 10));
    return date;
  }
  </code></pre>
  
  <p>and then extended the prototype</p>
  
  <pre><code>Date.prototype.isValid = function () {
    return !isNaN(this.getTime());
  }
  
  Date.prototype.clone = function () {
    return new _DefaultDateConstructor(this.getTime());
  }
  
  // More methods to return day name, month name etc. for formatting the display.
  </code></pre>
  
  <p>Which allows me do do this:</p>
  
  <pre><code>this.selectedDate = new Date(this.input.val());
  </code></pre>
  
  <p>where <code>this.selectedDate</code> is a valid javascript date I can work with, and <code>this.input</code> posts back correctly to my controller.</p>
  
  <p>When working in a test js file, all worked as intended, but when I copied the code into the plugin file,  it all crashed and burned. After much hair pulling, I realised that I had cut and pasted the constructor before the methods.  Once I put the constructor after the methods (as per the test file) it started working again.</p>
  
  <p>My first question is why was I getting <em>undefined is not a function</em> errors? Surely (but obviously not!) the overridden constructor is returning a javascript date object so the prototype methods should work if declared after the constructor.</p>
  
  <p>My second related question is, is there a way to have the overridden constructor 'private' to the plugin. If the plugin is loaded and you enter <code>myDate = new Date()</code> in the console, it returns <code>Invalid Date</code> (which is what I want for the plugin) but would be confusing for another user of the plugin.</p>