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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:23:36+00:00 2026-06-13T19:23:36+00:00

I am having a problem creating new instances of an object. Using the below

  • 0

I am having a problem creating new instances of an object.

Using the below code I was expecting each element to have it’s own random value (which is happening).

But then I was also expecting the this.element value to be contained to each instance of the object, but instead every time the value is changed in any instance of the object it is updating in all of them.

var Instance = function(element) {
    this.$element = $(element);
    this.subInstance.parent = this;
}

Instance.prototype = {

    subInstance: {

        parent: null,
        $element: null,

        init: function() {
            var $this = this;
            this.$element = this.parent.$element;

            //test for what this.$element refers to in init function
            var random = Math.random();
            $('.element', this.$element).text('Updated ' + random);

            //test for what this.$element refers to in an event handler
            $('.element', this.$element).on('click', function(e) {
                $this.$element.css('background-color', '#f00');
            });
        }
    }
}
$('div.instance').each(function(i, o) {
    var instance = new Instance(o);
    instance.subInstance.init();
});​

Now I know I could move subInstance out of the prototype and into the constructor using this.subInstance = {... but that seems wrong, why is this.$element not contained to each instance of the object?

JSFiddle of both examples: http://jsfiddle.net/q7zAg/

  • 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-13T19:23:38+00:00Added an answer on June 13, 2026 at 7:23 pm

    It may seem wrong, but it’s not. If each object created from the constructor needs to work with a unique subInstance, you’ll need to create a new one for each individual instance. On the prototype it will be shared.

    However, one thing you could do would be to use Object.create to create a new instance that inherits from the prototyped subInstance. Then you get the benefit of some reuse, and each instance can modify its own object.

    var Instance = function(element) {
        this.$element = $(element);
        this.subInstance = Object.create(this.subInstance);
        this.subInstance.parent = this;
    }
    

    Now some may argue that the subInstance still shouldn’t be on the prototype, but rather should be a local variable in an IIFE. I would tend to agree with this.

    Here’s an example:

    var Instance = (function() {
        var _subInstance = {
            parent: null,
            $element: null,
            init: function() {
                // ...
            }
        };
    
        var Instance = function(element) {
            this.$element = $(element);
            this.subInstance = Object.create(_subInstance);
            this.subInstance.parent = this;
        };
    
           // other prototyped props
        Instance.prototype.foo = "bar";
    
        return Instance;
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with Symfony creating a new session on each page load,
Having a bit of a problem creating an instance of an object. Bear in
I am creating an menu and having problem to access the element to hide
I'm having problems with the code below. Whenever I create a new instance of
I'm having trouble creating a new model row in the database using ActiveRecord in
I seem to be having a problem creating new local variables inside a switch
I'm having a problem creating a parent and child object and persisting them in
I'm new to Azure and I'm having a strange problem. I have a web
I'm having a problem with a generic class I'm creating in Scala. I have
I've got a problem where creating new drools StatefulKnowledgeSession instances is CPU intensive. There's

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.