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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:17:10+00:00 2026-06-02T01:17:10+00:00

Please help me decide whether I should use a function’s prototype object and the

  • 0

Please help me decide whether I should use a function’s prototype object and the “new” keyword or completely stay away from constructor functions.

Situation:

Function called widget() that will be called 10-15 times to initialize each widget on the page. widget() contains quite a few internal methods.

Each time widget() is called, the function needs to return an object that acts as an API to operate on the widget.

Question

1) Do I put all the internal methods inside Widget() under its prototype property? It does not make sense but the main reason for this is to not re-instantiate the internal functions every time widget() is called.

But if I do put the internal functions in prototype, each instantiated w object (w = new Widget();) has access to internal private methods.

2) If I stay away from constructor functions and new keyword and structure my code as down below, how do I fix the performance concern of the internal functions getting re-instantiated every time widget() is called.

function widget()
{
   var returnObj = {};

   /* Add internal functions but this will be re-instantiated every time */

   return returnObj;

}
  • 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-02T01:17:12+00:00Added an answer on June 2, 2026 at 1:17 am

    You have a bit of a tradeoff here. As you seem to already understand, methods you put on the .prototype are publicly available, but that is the most efficient places to put methods as they are automatically added to all new copies of that object in a very efficient manner. When using .prototype for methods, there is only one copy of your methods and a reference to that single copy is automatically added to all new instantiations of that object.

    But, javascript doesn’t have private methods built-in and the only work-around for that involves not using the .prototype for them or for any methods that need to call the private methods.

    This article by Doug Crockford is a pretty good description of how you can create privacy for either data or methods in any object.

    In either case, I don’t see any reason to avoid using the new keyword to create new objects. You can make either .prototype or private methods work with new.

    But, if you want to achieve truly private methods, then you can’t use .prototype for either the private methods or any methods that need to access them so you have to decide which is more important to you. There is no single correct answer because your need for privacy is situation-specific.

    In my coding, I generally don’t enforce privacy and I do use .prototype and new. I designate “non-public” methods on the prototype by starting their name with an underscore. This is a notational convention, not an access enforcement scheme.

    In answer to your second question about avoiding the new operator and reinstantiating methods, I’d just ask why you’re doing this? What are you gaining? I’m not aware of any downsides to using new. As best I understand your decision about whether to use .prototype vs. manually create/assign methods in your constructor should be about the need for private methods.

    FYI, 15 objects is hardly going to create a significant difference in performance either way here. I would evaluate your need for true privacy and make your decision based on that. If you HAVE to enforce privacy, then go with the Crockford method for implementing private methods. If you don’t HAVE to have true privacy, then use .prototype. I don’t see a reason here to avoid using new in either case.

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

Sidebar

Related Questions

Please help me with using the DrScheme built-in function filter. create a function hello
Please help to replace these jQuery functions with Prototype ones: $(button,input,label,select,textarea).bind('mouseover mouseout', function(){$(this).toggleClass('hover')}); $(button,input,select,textarea).bind('focus
I am making a web application using GWT. Please help me decide what I
Please help to the newbie in WPF! I need to build a TreeView with
Please help! I need to produce a Crystal Report with multiple data columns, but
Please help me to fix the CSS error in profile on my site (IE).
Please help me, how to set a background image for screen and How to
Please help me figure a single query that will transform the data below... |id
Please help I am getting the following error when I am binding the drop
Please help me out with an algorithm for the following problem - Given a

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.