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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:39:27+00:00 2026-05-29T19:39:27+00:00

I have a JavaScript object that does something like this – using a closure

  • 0

I have a JavaScript object that does something like this – using a closure to simulate private vs public functions/variables:

var myCoolObject = function(x, y) {

    var prop1 = "a cool prop1 value";
    var negX = x * -1;
    var negY = y * -1;
    var xyProduct = x * y;

    return {
      PublicProp1: prop1,
      getXYProduct: function() { return xyProduct; },
      getNegX: function() { return negX; },
      getNegY: function() { return negY; }
    }
}

I will be creating around 4000 instances of this object, and from what I am reading adding functions via the prototype will be more efficient than adding them as I have above (because in my example, each instance will have it’s own getXYProcust(), getNegX() and getNegY() functions.

My question is twofold – is my approach above really “inefficient”? I realize that inefficient is a relative term – but is this something I will likely notice. If it is inefficient, how would I add those functions to the prototype of myCoolObject? I tried the following:

myCoolObject.prototype.protoProp = "pppp";
myCoolObject.prototype.getAtMeBro = function () { return "get at me bro"; };
var myInstance = new myCoolObject(5, 10);

But neither protoProp nor ‘getAtMeBro()’ are properties of myInstance when I inspect it.

Thanks in advance for any help – I appreciate it!

  • 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-29T19:39:32+00:00Added an answer on May 29, 2026 at 7:39 pm
    1. The best advice is to try it and see. In the modern JS engines, you should find that 4,000 objects are childs play and should not be a problem (provided each of them don’t get too big). If you’ve got users on IE6 and older engines, be prepared for them to be able to cope with less.

      As an example, a Facebook Timeline feed requires ~4000 (3992 when I tried) DOM elements; each of these are far more complicated than the objects you’re creating (granted, they make use of the prototype method, but they hold a lot more information).

    2. You cannot apply the private member pattern when using prototype‘s, because you cannot create a closure to encapsulate both. The typical pattern here is to denote a private member with a leading _, to hint at people not to use it (but of course, there is nothing stopping them from doing so).

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

Sidebar

Related Questions

I have a javascript that does this (http is your XMLHttpRequest object) var r
I'm creating my own JavaScript Array-like object and I have methods that call closures.
Does jquery/javascript have any event model that you can attach to your objects? Basically
I have a Javascript object that requires 2 calls out to an external server
I have a Javascript object that basically represents a Row in an .NET GridView.
I currently have a Javascript function that uses a string to reference an object
I am trying to have a javascript object tree behave like a php associative
I have a rails template (.rhtml file) generating a Javascript object. It looks something
I have some ajax calls in multiple JavaScript functions. Each one does a post
We have some html that looks like this: <form id=MyUserControl runat=server method=post> <script language=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.