Define Private field Members and Inheritance in JAVASCRIPT module pattern
Asked Answered
B

1

3

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?

Brill answered 17/9, 2012 at 16:2 Comment(1)
possible duplicate of Private variables in inherited prototypesEmanuel
E
5

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}
});
Emanuel answered 17/9, 2012 at 16:17 Comment(3)
so effectively the first method?Brill
Yes - although I'd create full instances instead of returning object literals.Emanuel
…and the inheritance wouldn't work with .call if you return objects (just to mention)Emanuel

© 2022 - 2024 — McMap. All rights reserved.