如何在javascript中处理创建的类

如何在javascript中处理创建的类,javascript,class,Javascript,Class,在我的项目中,我有许多.js文件,我使用prototype.js将每个js文件制作成一个类。例如,我有以下js文件 one.js——类名为“one” two.js——类名为“two” three.js——类名为“three” Four.js——类名是“Four” 每个文件都依赖于其他js文件。因此,js文件的可能结构如下: four.js var Four = Class.create(); Four.prototype = { initialize : function(){

在我的项目中,我有许多
.js
文件,我使用
prototype.js
将每个js文件制作成一个
类。例如,我有以下js文件

  • one.js——类名为“one”
  • two.js——类名为“two”
  • three.js——类名为“three”
  • Four.js——类名是“Four”
每个文件都依赖于其他js文件。因此,js文件的可能结构如下:

four.js

var Four = Class.create();
Four.prototype = {
    initialize : function(){

    },
    one:   new One(),
    two:   new Two(),
    three: new Three(),
    firstMethod: function(){
        var one = this.one;    //Initializing
        var two = this.two;
        var three = this.three;
        //do operations using the above class variables
    },
    secondMethod : function(){
        var one = this.one;    //again Initializing
        var two = this.two;
        //do operations using the above class variables
    }
}
var Three = Class.create();
Four.prototype = {
    initialize : function(){

    },
    one:   new One(),
    two:   new Two(),
    four: new Four(),
    firstMethod: function(){
        var one = this.one;    //Initializing
        var two = this.two;
        var four = this.four;
        //do operations using the above class variables
    },
    secondMethod : function(){
        var one = this.one;    //again Initializing
        var two = this.two;
        //do operations using the above class variables
    }
}
three.js

var Four = Class.create();
Four.prototype = {
    initialize : function(){

    },
    one:   new One(),
    two:   new Two(),
    three: new Three(),
    firstMethod: function(){
        var one = this.one;    //Initializing
        var two = this.two;
        var three = this.three;
        //do operations using the above class variables
    },
    secondMethod : function(){
        var one = this.one;    //again Initializing
        var two = this.two;
        //do operations using the above class variables
    }
}
var Three = Class.create();
Four.prototype = {
    initialize : function(){

    },
    one:   new One(),
    two:   new Two(),
    four: new Four(),
    firstMethod: function(){
        var one = this.one;    //Initializing
        var two = this.two;
        var four = this.four;
        //do operations using the above class variables
    },
    secondMethod : function(){
        var one = this.one;    //again Initializing
        var two = this.two;
        //do operations using the above class variables
    }
}
正如您在上面的代码示例中所看到的,我使用类
Four
的不同方法初始化了类
One
两次。这看起来真的很糟糕,因为对于每个方法,我都在初始化已经初始化的同一个类

编辑:我还在js文件
three.js
three.js
中多次初始化
One
twree.js
类。上面的解释只是一个例子。实际上,在我的项目中,js文件有20多个

在我的项目中,我在所有js文件中遵循相同的结构

我应该如何使此结构高效


PS:一个更改较少的解决方案将非常有用,因为这是一个庞大且正在运行的项目,我现在不想冒险。

如果您需要在每个方法中使用不同的版本:

var Four = Class.create();

Four.prototype = {
  initialize : function(One, Two){
    // store for later use
    this.One = One;
    this.Two = Two;
  },
  firstMethod: function(){
      var one = new this.One(); // Initializing
      var two = new this.Two();
      //do operations using the above class variables
  },
  secondMethod : function(){
      var one = new this.One(); // again Initializing
      var two = new this.Two();
      //do operations using the above class variables
  }
};

// 'inject' needed class constructor functions
var four = new Four(One, Two);
如果您需要整个四个对象的单一版本:

var Four = Class.create();

Four.prototype = {
  initialize : function(One, Two){
    // initialize and use later this.Xxx
    this.One = new One();
    this.Two = new Two();
  },
  firstMethod: function(){
      //do operations using the above class variables
  },
  secondMethod : function(){
      //do operations using the above class variables
  }
};

// 'inject' needed class constructor functions
var four = new Four(One, Two);
编辑:

var Four = Class.create();

Four.prototype = {
  initialize : function(one, two){
    // initialize and use later this.xxx
    this.one = one;
    this.two = two;
  },
  firstMethod: function(){
      //do operations using the above class variables
  },
  secondMethod : function(){
      //do operations using the above class variables
  }
};

// 'inject' needed objects
var four = new Four(new One(), new Two());

为什么要为每个方法中的所有变量制作别名?例如,您的行
var one=this.one//正在初始化
。除了让您在函数的其余部分编写
one
而不是
this.one
。@meagar如果我在任何地方都使用
this.one
,那么它将为每个调用的方法初始化,这比现在发生的次数要多。这似乎不是真的吗?您在
one:new one(),
@meagar行初始化了一次,如果我再次调用它,那么它不会再次初始化吗?我想它会……不会。尝试一下,在
One
initialize方法中放置一个
console.log
。调用this.one不会初始化任何东西。它只访问已经初始化的变量。您不能使用不存在的代码。如果你想使用它,你必须加载它。我可以这样做吗<代码>变量四=新四(新一(),新二())。。如果我这样做有什么问题吗?谢谢,我看到你的答案后有了主意。我将在一个新的js文件中初始化所有这些类,该文件在dom就绪时运行。:)在这种情况下,我认为最好将参数作为对象传递:
var two=new two();var-four=4.initialize({two:two})内部
初始化:函数(params){this.one=params.one | | | new one();this.two=params.two | | new two()}