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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:41:47+00:00 2026-06-06T11:41:47+00:00

I am learning about JS Prototype. If I set a prototype of a constructor(A)

  • 0

I am learning about JS Prototype.

If I set a prototype of a constructor(A) from an instance of some other constructor(B), Is that instance (of B) would introduced shared properties in A?

Example 1

function A(){ 
    var private = '';

    this.getPrivate = function(){ 
        return private
    }; 

    this.setPrivate = function(_private){ 
        private = _private; 
    }; 
}

function B(){};

B.prototype = new A();

b1 = new B();
b2 = new B();

b1.setPrivate(new Date());
b2.getPrivate(); // Here `private` is behaving as singleton property. Why?

Example 2

function A(){ 
    var private = '';
}

A.prototype.getPrivate = function(){ 
    return this.private
}; 

A.prototype.setPrivate = function(_private){ 
    this.private = _private; 
}; 

function B(){};

B.prototype = new A();

b1 = new B();
b2 = new B();

b1.setPrivate(new Date());
b2.getPrivate(); // This time private is not singleton property.

I discovered this new aspect of prototype while playing with it.

  • In example 1, why private property is shared among different instances of B?
  • In example 2, why private property has independent presence in both instances? However original property is unchanged but the getter/setter are defined via prototype.
  • Can example 1 be considered an implementation of singleton properties?
  • Prototyping via instance and prototyping via prototype, What are the difference? e.g.
    B.prototype = new A();
    B.prototype = (new A()).constructor.prototype
  • What are the complete secrets of prototyping?
  • 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-06T11:41:49+00:00Added an answer on June 6, 2026 at 11:41 am
    1. Because you are inheriting a specific instance locked down to specific closures. The data is owned by
      the closures and not the object. There is a huge difference between variables and properties of object.

    2. The var private is not used here at all. When you setPrivate() the property is created at that point.

    3. No it’s just accidental because of misunderstanding between closures and object model. You can do the same
      in a much more deliberate and clearer way: by using a simple object literal.

    4. The difference is that in first one you get a new object, where as in second one you have A.prototype === B.prototype so modifying
      one will modify the other since they reference the same object.

    5. Objects inherit from other objects

    Here’s a good resource https://developer.mozilla.org/en/JavaScript/Guide/Details_of_the_Object_Model

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

Sidebar

Related Questions

In learning about Web Deploy I came across some netsh.exe commands that talk about
I am learning about XML Attributes from W3Schools . The author mentions the following
I'm learning about Memory management in C# from the book Professional C# The presence
Learning about threading is fascinating no doubt and there are some really good resources
I am facing a problem in my Spring learning and would need some help.
While learning about NSOperation, I wondered why a completion block would have any advantage
I've been learning about classes in JavaScript, the use of prototype and finally how
Learning about Explicit Cursors and trying to create my frst one.: SET SERVEROUTPUT ON
I'm learning about namespaces in PHP 5.3 and I would like to use Namespaces
I'm learning about Win32 programming, and the WinMain prototype looks like: int WINAPI WinMain

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.