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

  • Home
  • SEARCH
  • 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 6799427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:50:50+00:00 2026-05-26T18:50:50+00:00

Consider the following code. function a() {} function b() {} function c() {} b.prototype

  • 0

Consider the following code.

function a() {}
function b() {}
function c() {}

b.prototype = new a();
c.prototype = new b();

console.log((new a()).constructor); //a()
console.log((new b()).constructor); //a()
console.log((new c()).constructor); //a()
  • Why isn’t the constructor updated for b and c?
  • Am I doing inheritance wrong?
  • What is the best way to update the constructor?

Further, please consider the following.

console.log(new a() instanceof a); //true
console.log(new b() instanceof b); //true
console.log(new c() instanceof c); //true
  • Given that (new c()).constructor is equal to a() and Object.getPrototypeOf(new c()) is a{ }, how is it possible for instanceof to know that new c() is an instance of c?

http://jsfiddle.net/ezZr5/

  • 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-26T18:50:51+00:00Added an answer on May 26, 2026 at 6:50 pm

    Okay, let’s play a little mind game:

    From the above image we can see:

    1. When we create a function like function Foo() {}, JavaScript creates a Function instance.
    2. Every Function instance (the constructor function) has a property prototype which is a pointer.
    3. The prototype property of the constructor function points to its prototype object.
    4. The prototype object has a property constructor which is also a pointer.
    5. The constructor property of the prototype object points back to its constructor function.
    6. When we create a new instance of Foo like new Foo(), JavaScript creates a new object.
    7. The internal [[proto]] property of the instance points to the prototype of the constructor.

    Now, the question arises that why doesn’t JavaScript attach the constructor property to the instance object instead of the prototype. Consider:

    function defclass(prototype) {
        var constructor = prototype.constructor;
        constructor.prototype = prototype;
        return constructor;
    }
    
    var Square = defclass({
        constructor: function (side) {
            this.side = side;
        },
        area: function () {
            return this.side * this.side;
        }
    });
    
    var square = new Square(10);
    
    alert(square.area()); // 100
    

    As you can see the constructor property is just another method of the prototype, like area in the example above. What makes the constructor property special is that it’s used to initialize an instance of the prototype. Otherwise it’s exactly the same as any other method of the prototype.

    Defining the constructor property on the prototype is advantageous for the following reasons:

    1. It’s logically correct. For example consider Object.prototype. The constructor property of Object.prototype points to Object. If the constructor property was defined on the instance then Object.prototype.constructor would be undefined because Object.prototype is an instance of null.
    2. It’s treated no differently from other prototype methods. This makes the job of new easier since it doesn’t need to define the constructor property on every instance.
    3. Every instance shares the same constructor property. Hence it’s efficient.

    Now when we talk about inheritance, we have the following scenario:

    From the above image we can see:

    1. The derived constructor’s prototype property is set to the instance of the base constructor.
    2. Hence the internal [[proto]] property of the instance of the derived constructor points to it too.
    3. Thus the constructor property of the derived constructor instance now points to the base constructor.

    As for the instanceof operator, contrary to popular belief it doesn’t depend on the constructor property of the instance. As we can see from above, that would lead to erroneous results.

    The instanceof operator is a binary operator (it has two operands). It operates on an instance object and a constructor function. As explain on Mozilla Developer Network, it simply does the following:

    function instanceOf(object, constructor) {
        while (object != null) {
            if (object == constructor.prototype) { //object is instanceof constructor
                return true;
            } else if (typeof object == 'xml') { //workaround for XML objects
                return constructor.prototype == XML.prototype;
            }
            object = object.__proto__; //traverse the prototype chain
        }
        return false; //object is not instanceof constructor
    }
    

    To put it simply if Foo inherits from Bar, then the prototype chain for the instance of Foo would be:

    1. foo.__proto__ === Foo.prototype
    2. foo.__proto__.__proto__ === Bar.prototype
    3. foo.__proto__.__proto__.__proto__ === Object.prototype
    4. foo.__proto__.__proto__.__proto__.__proto__ === null

    As you can see, every object inherits from the Object constructor. The prototype chain ends when an internal [[proto]] property points to null.

    The instanceof function simply traverses the prototype chain of the instance object (the first operand) and compares the internal [[proto]] property of each object to the prototype property of the constructor function (the second operand). If they match, it returns true; and else if the prototype chain ends, it returns false.

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

Sidebar

Related Questions

Consider the following code: (function($) { function hello(who) { console.log('Hello: '+who); } })(jQuery); $(document).ready(function()
Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ //
Consider the following segment of code: function loadSomeContent() { URLLoader loader = new URLLoader(http://www.somesite.com/);
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Let's consider the following scenario: a function which can generate code colors from white
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following snippet of code: function parseXml(xml) { xmlObject= xml; alert(xmlObject.xml); } function
Consider the following code: var Widget = new Class({ Implements: [Options], options: { name
Consider the following code: class myclass { function __construct(&$arg1, &$arg2) { echo $arg1; echo
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()

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.