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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:37:32+00:00 2026-05-24T13:37:32+00:00

So, in answering another question on this site, I wrote a class for someone

  • 0

So, in answering another question on this site, I wrote a class for someone to create a BitArray instance in JavaScript. The code I posted looked like this:

var foo = function(param1) {
    this._member = param1;
    this.getMember = function() { return this._member; };
    this.setMember = function(val) { this._member = val; };
    this.alertMember = function() { alert(this._member); };
    foo.STATIC_CONSTANT = "some value";
}

One of the comments I received was a rather stern “You should absolutely add the methods to foo.prototype instead of this”. If I take that criticism and apply it to the above, it turns the code into:

var foo = function(param1) {
    this._member = param1;
}
foo.prototype.getMember = function(){ return this._member; };
foo.prototype.setMember = function(val) { this._member = val; };
foo.prototype.alertMember = function() { alert(this._member); };
foo.STATIC_CONSTANT = "some value";

My question is, what’s the difference here between the two approaches assuming foo will never be subclassed?

To be clear, I’m not advocating writing sloppy code – I credit the commentor with catching me being sloppy – but as I was conceeding to his correctness, it really did make me think – why should I prototype if I’m creating an effectively sealed class?

Is there some performance or functional consequence I’m missing here? Or when a class won’t be subclassed is using the prototype irrelevent?

  • 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-24T13:37:33+00:00Added an answer on May 24, 2026 at 1:37 pm

    There are various reasons. Firstly though I guess speaking of classes in JavaScript is misleading. There are no classes in JavaScript, but only prototypes.

    Let us analyze what actually happens in your first approach and what happens in the second approach. When the code of the first approach is run it defines a function, which, when it is called as a constructor, will create a bunch of other functions and assign them to members of the newly created object. You should be aware that this is done every time you call the function. So if you call the function once, you will have one object with a total of 4 functions. If you call the function twice, you will have two objects with a total of 8 functions, and so on. Each time, you create a new object using this constructor you will create a complete set of functions for it. Each of those function will require memory. Also, modern JIT compilers trying to spot hotspots in your code might fail to identify those functions as hot spots, since instead of having one function that is called multiple times you have several functions where each is only called once. Additionally even if the JIT is able to spot those functions as hotspots, it will have to compile each of those functions separately which will require additional work.

    That being said I believe the assumption “foo will never be subclassed” shouldn’t be valid. You cannot assume that others won’t want to inherit from your object or would like to augment the prototype of your object. This is currently not possible. Imagine I would like to change foo.STATIC_CONSTANT. I couldn’t, because each time I construct a new foo, it will override my changed foo.STATIC_CONSTANT again. Same is true for any other augmentation I might like to do.

    All these problems do not arise in the second, correct object oriented javascript pattern.

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

Sidebar

Related Questions

Answering another XSLT question on this site, I stumbled on a difference between XSLT
As part of answering another question, I wrote the following code whose behaviour seems
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
In answering this code golf question , I ran across a problem in my
This question arose when I was working on answering another question about best practices
As part of answering another question, I came across a piece of code like
I was answering another question about creating a binding in code-behind, and my original
While answering another question I bumped into this interesting situation Where WCF is happy
I ran into this question when i was answering another guys question. How do
When answering another question I started to wonder how I could Add new properties

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.