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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:14:32+00:00 2026-05-30T23:14:32+00:00

if I have myClass.prototype.subClass= { foo:false } when I create a new instance on

  • 0

if I have

myClass.prototype.subClass=
{
foo:false
}

when I create a new instance on an object how can I set foo to a different value each time, so how do I refer to that sub-class here

var newObj = new myclass();
  • 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-05-30T23:14:33+00:00Added an answer on May 30, 2026 at 11:14 pm

    Objects and arrays are not something which should be added to the prototype unless you want to share them with all instances.

    As soon as you want properties of these objects to be different for each instances, you have to assign the object in the constructor (or in any other function) to the specific instance:

    this.subClass = {
        foo: true
        // potentially other properties
    };
    

    That said, there might be cases were having a “default” object in the prototype might be reasonable, but you should not write to it.


    Assigning the object in the constructor instead does not duplicate code and allows you to change it for each instance individually.


    Update:

    If you don’t want to change the original constructor, you can either just add a new function to the prototype and call it whenever you instantiate an object:

    MyClass.prototype.init = function() {
        this.subClass = {
            //...
        };
    };
    

    and

    var obj = new MyClass();
    obj.init();
    

    Or you really create a new constructor function:

    function MySubClass() {
        MyClass.apply(this, arguments);
        // now create that object for each instance
        this.subClass = {
            foo: someValue
        };
    }
    
    inherits(MySubClass, MyClass);
    

    where inherits is defined as:

    function inherits(Child, Parent) {
        var Tmp_ = function() {};
        Tmp_.prototype = Parent.prototype;
        Child.prototype = new Tmp_();
        Child.prototype.constructor = Child;
    }
    

    Then you will use MySubClass instead of MyClass to create the instances.

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

Sidebar

Related Questions

I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Say I have MyClass with 100s of fields. If I use an object of
Using prototype.js, I create a custom object which is going to be a parameter
I have one javasctipc class... Lets call it X. myClass = new X(bunc, of,
The question is easy, suppose I have: class MyClass { object myObj1 object myObj2
Suppose I have a myClass < handle with property A . If I create
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have a class MyClass, and I would like to override the method ToString()
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =

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.