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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:26:22+00:00 2026-06-04T18:26:22+00:00

Suppose we have this constructor: var Foo = function(){ this.x = y; } Foo

  • 0

Suppose we have this constructor:

var Foo = function(){
   this.x = "y";
}

Foo and Foo.prototype.constructor evaluate to the same function, yet Foo.prototype.constructor = function(){this.x="z"} doesn’t seem to change the result of new Foo(). It does however change the result of

var i = new Foo();
i.constructor; // evals to function (){this.x = "z"}

What’s going on here? I don’t plan on using this for anything, I’m just curious about the language.

  • 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-04T18:26:24+00:00Added an answer on June 4, 2026 at 6:26 pm

    The constructor property of the prototype property of a function is meant to point back to the function so that you can ask an object what constructed it. It’s set up automatically as part of creating the function object (See Section 13.2 of the spec [or here for a more up-to-date version].) As you’ve seen, you can override the constructor property on the Foo.prototype if you like, to change that, but by default that’s what it’s for. For years the JavaScript specification only said that the constructor property would be there and have a given default value (function Foo() { } will mean that, by default, Foo.prototype.constructor is Foo.). But starting in ES2015, that changed, and various operations in the specification now actually use the constructor property, such as here, here, here, and here.

    (Note that the following was written before ES2015’s class feature was added. The below is how you’d do this in ES5 and earlier. In ES2015+, if you’re doing constructor functions and inheritance hierarchies, there’s no good reason to do the below; just use class. [If you don’t use constructor functions to build inheritance hierarchies — and you don’t have to, there are other ways to do them in JavaScript — you wouldn’t do the below or use class.])

    There’s a good reason you can override it, which relates to inheritance. Suppose you want to have a Base constructor that creates base objects, and a Derived constructor that creates derived objects with the features of Base plus the additions/modifications of Derived. The usual (though not to my mind ideal) way you see that done (absent helper scripts) is:

    function Base() {
    }
    Base.prototype.foo = function() {
        console.log("I'm Base#foo");
    };
    
    function Derived() {
    }
    Derived.prototype = new Base(); // So we get all the `Base` stuff
    Derived.prototype.bar = function() {
        console.log("I'm Derived#bar");
    };
    
    var d = new Derived();
    d.foo(); // "I'm Base#foo"
    d.bar(); // "I'm Derived#bar"
    

    The problem is that now, d.constructor === Base rather than Derived. So being able to fix that is important:

    ...
    Derived.prototype = new Base();                           // So we get all the `Base` stuff
    Object.defineProperty(Derived.prototype, "constructor", { // Fix up
        value: Derived,
        writable: true,
        configurable: true
    });
    ...
    

    (Side note: All of this plumbing — and complexity around supercalls — is why ES2015+ have class syntax.)


    Note that the above is not meant to be an ideal way to set up inheritance hierarchies. It’s what you usually see, but as I said above, not ideal. Just for completeness, in an environment limited to ES5 syntax, this is better:

    function Base() {
    }
    Base.prototype.foo = function() {
        console.log("I'm Base#foo");
    };
    
    function Derived() {
        Base.call(this); // So Base sets up its stuff
    }
    Derived.prototype = Object.create(Base.prototype); // So we get all the `Base` stuff
    Object.defineProperty(Derived.prototype, "constructor", {
        value: Derived,
        writable: true,
        configurable: true
    });
    Derived.prototype.bar = function() {
        console.log("I'm Derived#bar");
    };
    
    var d = new Derived();
    d.foo(); // "I'm Base#foo"
    d.bar(); // "I'm Derived#bar" 
    

    …where in a pre-ES5 environment, you use a shim/polyfill for Object.create. But again, I don’t do this directly (and don’t recommend it), I use helper scripts so it’s declarative and repeatable.

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

Sidebar

Related Questions

Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have this feature branch foo. Now I want to merge it back
Suppose I have this html markup: <div id=wrapper> <pre class=highlight> $(function(){ // hide all
Suppose you have a class with this constructor: public SomeObj(int x, int y) {
Suppose I have a function that tries to protect a global counter using this
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Suppose I have a the following in Scala object Foo { var functions: List[String
Suppose I have an object X defined as var X = function () {};
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have this: <select id=myselect> <option rel=a>1</option> <option rel=b>2</option> <select> How do I

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.