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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:38:38+00:00 2026-05-26T08:38:38+00:00

Consider the following to JavaScript snippets: function foo() { this.bar = function() { };

  • 0

Consider the following to JavaScript snippets:

function foo() {
  this.bar = function() { };
}

// or... (if we used an empty constructor function)

foo.prototype.bar = function() { };

What’s the difference when I do this:

function baz() {
}

baz.prototype = new foo();

In both cases baz ends up having a member bar but what’s the difference? Why would I do this in different places?

  • 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-26T08:38:38+00:00Added an answer on May 26, 2026 at 8:38 am

    The difference is where in the prototype chain the property is located.

    Assuming we have f = new foo(); and b = new baz(). Then we have the following situations:

    Definition of foo without using a prototype:

    +-----------+     +---------------+
    |     f     |     | foo.prototype |
    | __proto__-+---->| constructor   |  
    | bar       |     |               |
    +-----------+     +---------------+
    

    bar is a property of the object itself (f.howOwnProperty('bar') returns true).

    If you assign the property to the prototype instead, the situation is:

    +-----------+     +---------------+
    |     f     |     | foo.prototype |
    | __proto__-+---->| constructor   |  
    |           |     | bar           |
    +-----------+     +---------------+
    

    f does not have its own property bar, but the property is shared with all other foo instances.

    Similar for the second snippet, which results either in

    +-----------+     +---------------+     +---------------+
    |     b     |     | foo instance  |     | foo.prototype |
    | __proto__-+---->| __proto__    -+---->| constructor   |  
    |           |     | bar           |     |               |
    +-----------+     +---------------+     +---------------+
    

    or

    +-----------+     +---------------+     +---------------+
    |     b     |     | foo instance  |     | foo.prototype |
    | __proto__-+---->| __proto__    -+---->| constructor   |  
    |           |     |               |     | bar           |
    +-----------+     +---------------+     +---------------+
    

    Why do you want to do this?

    It’s mainly about structure and not wasting memory. You can add functions to an object in a constructor function:

    function Foo() {
        this.bar = function() {};
    }
    

    but this also means that each instance of Foo has it’s own function, that is, f1.bar === f2.bar is false, although both functions are doing the exact same thing.

    Using the prototype gives you a clean way to separate properties common to all instances and instance-specific ones.

    In the end, it is “just” inheritance which is one concept in software development (like aggregation) and can be used wherever it makes sense. Your second snippet basically means that a baz is-a foo, so a baz instance, in addition to its own properties, has the same properties as a foo instance (inherited).

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

Sidebar

Related Questions

Consider the following Javascript function (1): function setData(domElement) { domElement.myDataProperty = { 'suppose': 'this',
Consider the following JavaScript code which has a recursive boom function function foo() {
Consider the following document <html> <body> This is some content <script type=text/javascript src=verySlowToRespond.js></script> This
Consider the following Javascript code: var a = []; var f = function() {
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)
Consider the following top-level javascript code: if (this.window === window) alert('same'); else alert('different'); //
Consider the following HTML: (Written for Google Chrome) <html> <head> <script language=javascript> function AddToIFrame(){
This is mostly an out-of-curiosity question. Consider the following functions var closure ; function
I'm searching for a JavaScript template that allows function evaluating. Consider the following JSON:
Possible Duplicate: Can a JavaScript function return itself? Consider the following javascript function: var

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.