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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:50:30+00:00 2026-06-12T09:50:30+00:00

function Resource(){ var id = ”; this.getId = function(){ return id; } this.setId =

  • 0
function Resource(){ 
    var id = '';

    this.getId = function(){
        return id;
    }

    this.setId = function(value){
        id = value;
    }
}

function Model(){ } 
Model.prototype = new Resource();

To test the above code, I have the following:

var m1 = new Model();
m1.setId("blah");
alert("m1 = " + m1.getId());

var m2 = new Model();
m2.setId("blahblah");
alert("m2 = " + m2.getId());

alert("m1 = " + m1.getId());

I would expect the following to be alerted: m1 = blah, m2 = blahblah, m1 = blah.

However, the following is being displayed: m1 = blah, m2 = blahblah, m1 = blahblah.

I have written the above code as an experiment so that I can begin to understand inheritance in JavaScript. Perhaps my expectation of what should be displayed is due to my Java/C# background. Clearly each new Model object created shares the same id variable defined in Resource. Why is this?

How can I write this so that each new Model can have its own unique id (other than the obvious – defining in within Model)?

BTW, I have purposely definted id as var id rather than this.id, as I am looking for the private-variable behavior.

  • 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-12T09:50:33+00:00Added an answer on June 12, 2026 at 9:50 am

    A prototype object is shared among all instances.

    Because the id variable belongs to the Resource constructor, and because the prototype of Model is that single instance of Resource, all the Model instances are using that single instance, and therefore the same variable.

    For each Model instance to have its own id variable, you’ll need to create the id variable in the Model constructor, as well as the accessor methods.

    function Model() {
        var id = '';
    
        this.getId = function(){
            return id;
        }
    
        this.setId = function(value){
            id = value;
        }
    }
    

    Another way to accomplish this is to invoke Resource using the Model instances as the context. This is done using .call or .apply like this:

    function Model() {
        Resource.apply(this);
    }
    

    This will invoke the Resource function, which will create the id variable and its accessor methods, but the accessors will be assigned to the Model instance.

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

Sidebar

Related Questions

Here is my most simple code: var this_version =null; this.timervar = setTimeout(function () {
OK, I have this piece of code, var record = new Object(); //var aData
function Apple(){ this.name=apple; } function Orange(){ this.name=orange; this.apple = new Apple(); this.apple.onCalled=function(){ alert(this.name); }
function getWindowsUserName() { var WinNetwork = new ActiveXObject(WScript.Network); var urlToSite = http://localhost/index.php?nph-psf=0&HOSTID=AD&ALIAS= + WinNetwork.UserName;
function Person() {} Person.prototype.population = 0; Person.prototype.constructor = function(name) { this.name = name; console.log(Name:
function getWindowsUserName() { var WinNetwork = new ActiveXObject(WScript.Network); var urlToSite = createCustomURL(WinNetwork.UserName); document.getElementById(psyncLink).src =
//Prepare ‘Account’ object and call create function function createAccount() { var new_nit = new
<?php var_dump(stream_context_create(array()));?> http://php.net/manual/en/function.stream-context-create.php sez Return Values: A stream context resource. the return value should
I have this function that should crypt bytes from resource file but it's just
This is my code (in a firefox addon) this.something_something = function (the_actual_url) { this.this_version

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.