In my project, I have many .js files, I made each js file as a Class using prototype.js. For example, I have the following js files.
- one.js -- Class name is "One"
- two.js -- Class name is "Two"
- three.js -- Class name is "Three"
- Four.js -- Class name is "Four"
Each file depends on other js files. So, the possible structure of js files is something like this:
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
}
}
three.js
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
}
}
As you can see in the above code example, I am Initializing the class One two times in different methods of the class Four. This seems really worst because for every method I am initializing the same class which was already initialised.
EDIT: And also I am initializing One and Two classes more than once in js files three.js and four.js. The above explanation is just for example. Actually, in my project, there are js files more than 20.
In my project, I am following the same structure in all js files.
How should I make this structure efficient?
PS: A solution with less changes would be really helpful because this is a huge and running project and I don't want to take risks at this time.
var one = this.one; //Initializing. That isn't doing anything, except letting you writeoneinstead ofthis.onein the rest of the function.this.oneeverywhere then it will initialize for every called method which is more times than it is happening now.one: new One(),.console.login yourOneinitialize method. Invokingthis.onedoesn't initialize anything. It just accesses the already initialized variable.