Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

oop - Define Private field Members and Inheritance in JAVASCRIPT module pattern

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...