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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:52:51+00:00 2026-06-14T19:52:51+00:00

Possible Duplicate: Why is it necessary to set the prototype constructor? I’m struggling to

  • 0

Possible Duplicate:
Why is it necessary to set the prototype constructor?

I’m struggling to understand the necessity of setting the ‘constructor’ property of a javascript object to the subclass when building a hierarchy. I find that the code below does what is expected without changing the constructor property, but in almost all references I find about the subject the constructor is set explicitly. Am I missing something ? (I don’t find any explicit use of it in the ECMAScript specs either).

A = function() {
    this.value = "a";

    this.A = function() {
        window.alert( this.value + " instanceof A : " + ( this instanceof A ) );
    }
}


B = function() {
    this.value = "b";

    this.B = function() {
        window.alert( this.value + " instanceof B : " + ( this instanceof B ) );
    }
}

B.prototype = new A();

test = function() {
    var b = new B();
    b.A();
    b.B();
}
  • 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-14T19:52:52+00:00Added an answer on June 14, 2026 at 7:52 pm

    First of all, proper JS inheritance means putting methods in the prototype:

    var A = function() {
        this.value = "a";
    };
    
    A.prototype.A  = function() {
            window.alert( this.value + " instanceof A : " + ( this instanceof A ) );
    };
    
    var B = function() {
        this.value = "b";
    };
    

    Secondly, don’t run the constructor when establishing a prototype chain:

    B.prototype = Object.create( A.prototype );
    

    Whenever you reassign the entire .prototype, you are completely overwriting the object. So the constructor
    property needs (If it’s going to be used) to be reassigned:

    B.prototype.constructor = B;
    
    B.prototype.B = function() {
        window.alert( this.value + " instanceof B : " + ( this instanceof B ) );
    };
    

    Object.create is not supported in older browsers but you can do something like:

    Object.create = Object.create || function( proto ) {
         if( proto == null ) {
             return {};
         }
         function f(){}
         f.prototype = proto;
         return new f();
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Any sense to set obj = null(Nothing) in Dispose()? I understand if
Possible Duplicate: Garbage Collection: Is it necessary to set large objects to null in
Possible Duplicate: Can I fetch the value of a non-standard CSS property via Javascript?
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate
Possible Duplicate: Which maven2 artifacts are necessary to build a WS with CXF and
Possible Duplicate: PHP_SELF and XSS Why it's necessary to filter $_SERVER['PHP_SELF'], from e.g.: <form
Possible Duplicate: Setting Objects to Null/Nothing after use in .NET Do you need to
Possible Duplicate: Why setting to nil after releasing? Which of the following examples are

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.