在JAVASCRIPT模块模式中定义私有字段成员和继承 [英] Define Private field Members and Inheritance in JAVASCRIPT module pattern

查看:125
本文介绍了在JAVASCRIPT模块模式中定义私有字段成员和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用下面的代码在模块模式中定义私有成员字段

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

此方法工作得很好,但这个问题的问题在于每当我创建一个新对象时所有函数的副本都在内存中创建并加载(不像java那样,同一类的所有对象共享相同的函数内存)

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

但是这个方法的问题是显然obj1,obj2共享私有字段的相同副本(因此有效他们是静态的)。那么有没有办法在模块模式中定义私有字段,同时为对象使用相同的函数副本?

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

但实际上这只是在childClass中包装baseClass的对象,是否有一些实现相同的方法(第一种或第二种方法),以便它可以像保护,私有等方法一样真正的java继承?

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?

推荐答案

没有。 JavaScript中的私有性只能通过作用域(并从中导出:闭包)来完成。

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.

顺便说一句,不是函数[code]本身被复制和记忆多次。只需要存储不同的范围对象(词法环境)。

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"
}

此外,Child的原型直接从Parent的原型对象继承。这可以通过 Object.create 来完成(需要为旧版浏览器填充):

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

这篇关于在JAVASCRIPT模块模式中定义私有字段成员和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆