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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:30:49+00:00 2026-06-11T14:30:49+00:00

I can define private member fields in module pattern using the code below var

  • 0

I can define private member fields in module pattern using the code below

    var myClass = function(){
       var private_field1,private_field_2;
       var private_func1 = function(){
            //.......
       } 
       //.........
       var myObj = {
         global_field1:2,
         global_field2:"something",
         global_func: function(){//......} 
       }
       return myObj;
    };
   var obj = myClass();

This method works just fine, but the problem with this problem is that whenever I create a new object the copy of all the functions is created and loaded in memory (not like java where all objects of same class share same function memory)

I tried to use other method below:

 var myClass = (function(){
           var private_field1,private_field_2;//private static fields
           var private_func1 = function(){
                //.......
           } 
           //.........
           var Constr = function(){
             //do something
           }
           Constr.prototype = {
             //................
             global_func: function(){//......} 
           }
           return Constr;
    }());
var obj1 = new myClass();
var obj2 = new myClass();

But the problem with this method is that obviously obj1,obj2 share same copy of private fields(so effectively they are static). So is there a way to define private fields in module pattern while using same copy of functions for the objects?

And for inheritance for the first method mentioned above, i first need to create a object inside the child class and then return that object.

 var ChildClass = function(){
      var childobj = myClass();
      //override or add functions to childobj
      return childobj ;
 }

But this is effectively just wrapping the object of baseClass in childClass, Is there some other way to implement the same(for 1st or 2nd method) so that it can act like true java inheritance with protected, private, etc methods?

  • 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-11T14:30:51+00:00Added an answer on June 11, 2026 at 2:30 pm

    No. Privateness in JavaScript can only be done by scoping (and exporting from them: closures).

    Those functions that need to access the private variables (and functions), called privileged methods need to be defined inside the constructor. Those methods that don’t (which only interact with public properties or other methods) should be defined on the prototype object, so you will get a mixed approach in the end. Potentially combined with the static values you just discovered.

    Btw, not the function [code] itself is copied and memorized multiple times. Only different scope objects (lexical environments) need to be stored.

    Inheritance is usually not done by creating parent objects and extending them, but by creating child instances and extending them like a parent. This is can be done by applying the parent’s constructor on the newly created child:

    function ChildClass() {
        ParentClass.call(this, /* … arguments */);
    
        // usual body, using "this"
    }
    

    Also, the prototype of the Child inherits directly from the Parent’s prototype object. This can be done via Object.create (needs to be shimmed for legacy browsers):

    ChildClass.prototype = Object.create(ParentClass.prototype, {
        constructor: {value:ChildClass}
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I define a background worker in a method ? private void DownLoadFile(string fileLocation){
I can define church numerals fairly easy using scheme: > (define f (lambda (x)
When a static member variable is declared private in a class, how can it
I have a class myclass that has a private member param_map class some_class {
I can define default value in domain by this way : class ProcessingPriority {
I know I can define default parameters in python, but can I do so
I know I can define new structs in ruby by doing Person = Struct.new(:first_name,
In HTML/CSS you can define a style which can be applied to many types
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
In C# you can define delegates anonymously (even though they are nothing more than

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.