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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:41:05+00:00 2026-05-26T05:41:05+00:00

I was reading on javascript garden http://bonsaiden.github.com/JavaScript-Garden/ about prototype in javascript and one of

  • 0

I was reading on javascript garden http://bonsaiden.github.com/JavaScript-Garden/ about prototype in javascript and one of its example goes like this:

function Foo() {
    this.value = 42;
}
Foo.prototype = {
    method: function() {}
};

function Bar() {}

// Set Bar's prototype to a new instance of Foo
Bar.prototype = new Foo();
Bar.prototype.foo = 'Hello World';

// Make sure to list Bar as the actual constructor <-------------------
Bar.prototype.constructor = Bar;

Notice the line that reads Make sure to list Bar as the actual constructor. I really am lost about what this does/is. I have tried making new instances of Bar() with and without the last line. But call “value” or “method” on those instances return the exact same things. So I wonder, what’s the need (I assume there must be one) of specifying the constructor?

Thank You!!!

  • 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-26T05:41:06+00:00Added an answer on May 26, 2026 at 5:41 am

    Every function has a prototype property, it’s assigned when the function object is created, it points to a newly created object that inherits from Object.prototype, and it has a constructor property, that simply points back to the function itself.

    The purpose of the prototype property is to give a way to implement inheritance, using constructor functions. When you call a function with the new operator, it will create a new object that inherits from that constructor’s prototype.

    Now, the purpose of the constructor property is to have a way to refer back to the constructor that has created an object, for example:

    function Foo () {}
    // default value of the property:
    Foo.prototype.constructor == Foo; // true
    

    This property is inherited by the “instances” of Foo, so you can know which constructor was used to create an object:

    var foo = new Foo();
    foo.constructor == Foo;
    

    If you assign a new object to the function’s prototype, this relationship is lost:

    function Bar () {}
    Bar.prototype = { inherited: 1 };
    
    Bar.prototype.constructor == Bar;    // false
    Bar.prototype.constructor == Object; // true
    

    And it affects also the instances of the function:

    var bar = new Bar();
    bar.constructor == Bar;    // false
    bar.constructor == Object; // true
    

    Another similar case is when you have two or more levels of inheritance using constructors, the most common way is used to denote the inheritance relationship between the functions, is to assign the prototype property of the second level, e.g.:

    function Parent() {}
    
    function Child () {}
    Child.prototype = new Parent();
    

    The above code has several problems, first, it executes the logic of the parent constructor to create the inheritance relationship, but that’s another story, in the above example the constructor property is also affected, since we replace completely the Child.prototype object:

    var child = new Child();
    child.constructor == Parent; // true
    

    If we replace replace the value of the constructor property of Child.prototype after assigning it, it will show the expected behavior:

    function Child() {}
    Child.prototype = new Parent();
    Child.prototype.constructor = Child;
    
    var child = new Child();
    child.constructor == Child; // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading http://www.pragprog.com/magazines/2010-03/javascript-its-not-just-for-browsers-any-more I'm wondering which is the best IDE to develop server-side javascript
I was reading up on this javascript tutorial: http://www.switchonthecode.com/tutor...ccordion-menus Basically, it shows you how
While reading a book about JavaScript I stumbled across an example: var names =
I was reading Eloquent JavaScript and I came across this example for the puzzle:
I've been playing around with prototypal inheritance after reading http://javascript.crockford.com/prototypal.html and having a bit
Reading through this question on multi-threaded javascript, I was wondering if there would be
I was reading about output buffering in JavaScript here , and was trying to
I was reading through this thread: Hidden Features of JavaScript? and found this post:
I'm just reading about Prototypes in JavaScript and Douglas Crockford offers and excellent way
I am reading Javascript The Definitive Guide. In this book, Events and Event handling,

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.