Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:38:08+00:00 2026-06-08T18:38:08+00:00

How can I add data/functions to all instances of a javascript object created by

  • 0

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?

Basically implementing the equivalent of a static method in C#.

For example, given the following code which creates a Widget class.

(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

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.

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?

(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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:38:09+00:00Added an answer on June 8, 2026 at 6:38 pm

    Three pitfalls that I can think of:

    • methodological: the prototype is the place for shared, reused, inherited functionality/properties – utilise it as such

    • performance: it is quicker to inherit than to set each time on an instance. John Resig (jQuery creator) did some benchmarking on this in a blog post that I appear unable to find at present.

    • losing the split between inherited and own properties. If you apply everything to an instance via the constructor, everything is an instance property.

    Everything via constructor:

    function Dog() { this.legs = 4; }
    var fido = new Dog();
    fido.name = 'Fido';
    for (var i in fido) if (fido.hasOwnProperty(i)) alert(i+' = '+fido[i]);
    

    …alerts both properties as they are deemed the instance’s own.

    Via prototype and constructor

    function Dog2() { }
    Dog2.prototype.legs = 4;
    var fido = new Dog2();
    fido.name = 'Fido';
    for (var i in fido) if (fido.hasOwnProperty(i)) alert(i+' = '+fido[i]);
    

    …alerts just name because that is the only instance property. (Nonetheless, fido.legs is retrievable – but it comes from the prototype).

    [EDIT – in response to the OP’s commet below]

    If you want a static method, then that should be added to the function after its declaration.

    function Dog() {}
    Dog.static = function() {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way that I can add non-static data to dataannotation attributes (either
is it possible to add custom data to the results so it can be
How can I use data binding on a details form to add a new
While we can add Inline Images in paragraphs there does not appear to be
I have a DataSet that I have added to my project where I can
I have a launchd daemon that every so often uploads some data via a
first of all sorry for my English :-) not so good. I have a
Part of what's so powerful about Clojure is that all the core data-types implement
I can add and remove the last line in my dynamic form and calculate
I can add a normal rightBarButton to my navigation without a problem but now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.