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 7746491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:17:30+00:00 2026-06-01T10:17:30+00:00

In javascript if I need objects each to have an individual value of an

  • 0

In javascript if I need objects each to have an individual value of an attribute, I can just set it in the constructor, like so:

function A(x) {
    this.a = x;
}

If I want a certain attribute to be shared among the objects, I can set it in the prototype of constructor function.

function B() {}
B.prototype.b = 'B0';

But what to do in in-between situation? Basically, I have an existing code where all the constructed objects inherit a property from a prototype, but now I need to divide them into several groups so all members of a group share an attribute.

Is there a way to specialize the constructor function B somehow?

  • 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-01T10:17:31+00:00Added an answer on June 1, 2026 at 10:17 am

    B.prototype.b does NOT create a static property as you presume. It’s a bit more complicated than that, properties attached to a prototype share their value with other instances, until they overwrite that value, meaning that:

    var Foo = function(){
    };
    Foo.prototype.bar = 'bar';
    
    var f1 = new Foo();
    console.log( f1.bar ); //outputs 'bar'
    var f2 = new Foo();
    console.log( f2.bar ); //outputs 'bar'
    
    f2.bar = 'notbar';
    console.log( f2.bar ); //outputs 'notbar'
    console.log( f1.bar ); //outputs 'bar'
    

    The only way to have “real” static properties is to attach them to the constructor function itself:

    Foo.bar2 = 'bar2';
    

    instances of Foo will have to access that value with Foo.bar2.

    So the answer to your question is to create “subclasses” (constructor functions that inherit their prototype from a base constructor function) for each group and attach a property per subclass, like this:

    var Base = function(){
    };
    Base.prototype.getSomething = function(){
        return this.constructor.something;
    };
    Base.prototype.setSomething = function( value ){
        this.constructor.something = value;
    }
    var Group1 = function(){
    };
    Group1.prototype = new Base(); //classical inheritance
    Group1.prototype.constructor = Group1;
    Group1.something = 'something';
    var Group2 = function(){
    };
    Group2.prototype = new Base(); //classical inheritance
    Group2.prototype.constructor = Group2;
    Group2.something = 'something else';
    
    var g1a = new Group1();
    var g1b = new Group1();
    var g2a = new Group2();
    var g2b = new Group2();
    
    g1a.setSomething( 'whatever' );
    
    console.log( g1a.getSomething() ); //outputs 'whatever'
    console.log( g1b.getSomething() ); //outputs 'whatever'
    console.log( g2a.getSomething() ); //outputs 'something else'
    console.log( g2b.getSomething() ); //outputs 'something else'
    

    Warning: Group1.prototype = new Base(); is actually bad practice, I wrote a blog post about 3 types of inheritance just a few days ago which explains why:

    http://creynders.wordpress.com/2012/04/01/demiurge-3-types-of-javascript-inheritance-2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript array of objects like this: box[0] = {...} box[1] =
I'd like to have JavaScript objects within another JavaScript object as such: Issues: -
I need to temporarily track the state of 5 objects in JavaScript. Each of
I have two objects defined something like this (simplified for sake of the question):
In Javascript I need to order objects in an array according to type. Each
In javascript I need to know if array contains value. Values are objects and
I often have the need to search a javascript array that contains objects. I
I need a random object generator in JavaScript that generates a variety of objects
I am using jQuery to save the values of my javascript objects. I need
What are expando objects in javascripts? For what purpose we need this ? Any

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.