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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:49:36+00:00 2026-06-03T05:49:36+00:00

How do you make a Javascript private method that is not redefined each time

  • 0

How do you make a Javascript private method that is not redefined each time you call the constructor ?

As far as I know, in OOP-JS, private methods are methods defined in the “constructor method” of one’s “class”, called each time one instantiates a new “object”. I was thinking maybe a function declaration (i.e. function name(), as opposed to function expression var name = function()) would do the trick, but how can I be sure that the following code only declares my function once ?

​function Tester() {
    function test () {
        console.log("executed");
    }
}
var t1 = new Tester();
var t2 = new Tester();
  • 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-03T05:49:37+00:00Added an answer on June 3, 2026 at 5:49 am

    How do you make a Javascript private method that is not redefined each time you call the constructor ?

    You can’t (well, see below for a bit of wiggle room). But unless you’re going to have thousands of instances of Tester, don’t worry about it too much; most engines probably reuse the underlying code across the multiple function objects that get created. (The code, mind; not the function object or the context it closes over, which must be unique and allocated each time. But they need not be large. Of course, quite a function functions are fairly small as well…)

    …how can I be sure that the following code only declares my function once ?

    You can be sure that it doesn’t; it declares the function each time Tester is called. Witness:

    ​function Tester() {
        this.test = test;
        function test () {
            console.log("executed");
        }
    }
    var t1 = new Tester();
    var t2 = new Tester();
    console.log(t1.test === t2.test); // "false"
    

    Note that you can have functions that are private to the implementation, but not assigned to any instance of the object. The module pattern is handy for doing that:

    var Tester = (function() {
    
        function Tester(name) {
            this.name = name;
        }
        Tester.prototype.publicFunction = function() {
            privateFunction.call(this);
        };
    
        function privateFunction() {
            console.log("My name is " + this.name);
        }
    
        return Tester;
    })();
    
    var t = new Tester("Fred");
    t.publicFunction(); // Outputs "My name is Fred" via the private function
    

    There, privateFunction is completely private, accessible only to the code within the anonymous function. And there’s only one copy of it, but you can call it as though you were calling a method of a Tester instance using privateFunction.call(this).

    Alternately, of course, since using call is slightly slower than doing a normal call, you could just pass the instance as an argument:

    var Tester = (function() {
    
        function Tester(name) {
            this.name = name;
        }
        Tester.prototype.publicFunction = function() {
            privateFunction(this);
        };
    
        function privateFunction(t) {
            console.log("My name is " + t.name);
        }
    
        return Tester;
    })();
    
    var t = new Tester("Fred");
    t.publicFunction(); // Outputs "My name is Fred" via the private function
    

    Of course, the extra cost of call is only a problem if and when it’s a problem; unless you’re calling something hundreds of thousands of times in a tight loop, it’s unlikely to matter. So whether to use call and this or pass an argument would be primarily a style choice.

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

Sidebar

Related Questions

I have a method: myMethod() {} that I want to make accessible to javascript.
Is it possible to make JavaScript retrieve a file that is above the web
How to call or make the javascript function from the Application_Start of global.asax in
I'm trying to make a Javascript function that will take a mathematical expression and
To make a JavaScript class with a public method I'd do something like: function
I have a script that forces a download and I make a call to
I make an AJAX call from JavaScript client (running on machine A) to Web
I was wondering how I could make private variables in javascript through clojure. But
I need to make javascript code to swap images with option change. Here is
I have to use javascript to make links instead of for several unimportant reasons,

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.