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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:13:16+00:00 2026-06-11T19:13:16+00:00

What exactly is the difference between function ObjA() { this.a = ‘text’; } var

  • 0

What exactly is the difference between

function ObjA() {
  this.a = 'text';
}

var obj = new ObjA();

and

function ObjB() {
  return {
    a: 'text'
  };
}

var obj = new ObjB();

I’m asking because I was reading this question and I’ve noticed the following in one of the answers:

function Test() {
  this.a = 1;

  return {
    get A() { return this.a; },
    set A(v) { this.a = v; }
  };
}

so I asked myself what is the difference between that and the following:

function Test() {
    this.a = 1;
}

Test.prototype = { 
    get A() { return this.a; },
    set A(v) { this.a = v; }
};

They say in the comments that the former “hogs up memory as the getter and setter is “unique” per each object”. Could someone please elaborate on this?

  • 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-11T19:13:18+00:00Added an answer on June 11, 2026 at 7:13 pm

    You’re actually asking a couple different questions. So let me concentrate on the first one (modifying the variable names to more easily refer to them and not overwrite Object):

    What exactly is the difference between:

    function ObjA() {
      this.a = 'text';
    }
    
    var objA = new ObjA();
    

    and

    function ObjB() {
      return {
        a: 'text'
      };
    }
    
    var objB = new ObjB();
    

    The difference is that the former version maintains the prototype chain, whereas the later version discards it. Consider the following lines of code:

    ObjA.prototype.b = "something";
    ObjB.prototype.b = "something";
    

    The following then becomes true:

    objA.b; //is "something"
    objB.b; //is undefined
    

    The reason is that the object returned from the “constructor” does not append ObjB’s prototype chain. It’s a brand new object. So that’s the “difference.”

    The second example (using .prototype vs. returning an object) actually doesn’t really “waste” memory as far as I know (see update). Because the process of calling the new operator on a function will create a copy of the object’s prototype and then call its function. The nice part is that the prototyped methods will be available within the “constructor” function when you use .prototype, and you’ll maintain the prototype chain with that version. But I don’t know that there’s anything really “wrong” about using the return-based method.

    UPDATE:

    I checked out the ECMAScript spec on the subject (and oiled my thinking gears a bit) and it appears that I was wrong about the memory wasting. It looks like the methods/properties of the “class” function’s prototype property are linked by reference. So it does actually waste a bit of memory to generate a new object rather than making use of the prototype. Additionally, any properties declared in the returned object are instance-level, whereas properties declared in the prototype object are static to the “class” (i.e. shared by all instances).

    And as others have noted, your example has a small bug (a is not available to the prototype object). But that’s somewhat immaterial to the question at hand, so I ignored it. (I see you fixed the bug).

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

Sidebar

Related Questions

What is the fundamental difference between using $(this) vs this $('.viewComments').click(function(ev){ //returns the desired
foo(); (*foo)(); (&foo)(); What exactly is the difference between these function calls (assuming foo()
Possible Duplicate: The difference between the two functions? (“function x” vs “var x =
What exactly is the difference in rails between dev and prod environments. When I
What exactly is the difference between array_map , array_walk and array_filter . What I
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
What exactly is the difference between shuffle and array_rand functions in PHP? Which is
Difference between the two functions: (function($){ console.log($); }(test)); (function($){ console.log($); })(test); I've tried it
1.) What's the difference between these two queries, exactly? $( #orderedlist li ) $(
Possible Duplicates: What is the difference between a function expression vs declaration in JavaScript?

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.