How to add data/functions to all instances of a javascript object created by a constructor?
<p>How can I add data/functions to all instances of a javascript object created by a constructor so that all instances have the same reference and not a copy of it?</p>

<p>Basically implementing the equivalent of a static method in C#.</p>

<p>For example, given the following code which creates a Widget class.</p>

<pre><code>(function() {

    var Widget = function() {
    };

    Widget.prototype.init = function(data) {
        this.data = data;
    };

    this.Widget = Widget;
}).call(this);
var instance1 = new Widget();
instance1.init('inst1');
var instance2 = new Widget();
instance2.init('inst2');

alert(instance1.data); // inst1
alert(instance2.data); // inst2
</code></pre>

<p>In the above case each instance has it's own copy of the data property. However I want to add a function that sets data for all current and future instances.</p>

<p>My current solution is to add a function to the constructor function object, not to it's prototype. See below for example. Is there any pitfalls to this and is there a better way?</p>

<pre><code>(function() {

    var Widget = function() {
    };

    Widget.prototype.init = function(data) {
        this.data = data;
    };

    Widget.addStaticData = function(data) {
        this.staticData = data;
    };

    Widget.prototype.getStaticData = function() {
        return Widget.staticData;
    };

    this.Widget = Widget;
}).call(this);
var instance1 = new Widget();
instance1.init('inst1');

Widget.addStaticData('static');

var instance2 = new Widget();
instance2.init('inst2');

alert(instance1.data); // inst1
alert(instance2.data); // inst2
alert(instance1.getStaticData()); // static
alert(instance2.getStaticData()); // static
</code></pre>
