I am thinking about passing a private variable of one class into another class, for some belonging case like 'company --> department --> employee'. As an employee can tell some information of his company, there's some private variables I'd like to pass.
I think this relationship is not as same as common class and supClass. As an employee is not a type of department and a department is not a type of company (if I am wrong, please tell me more).
So in a case like this, is it a good idea to use nested constructor functions?
For example: http://jsfiddle.net/JsJJ9/
function TeamClass(name){
var pass = "privateVariable"
var slot = 1;
this.members = []; // I have a list of members
this.name = name;
this.add = function(){ //add member into this team
var m = new MemberClass(slot++);
this.members.push(m)
}
function MemberClass(id){ //here comes the Nested class
var id = id;
this.report = function(){
alert('#'+id+' : Hello, i got some '+pass+'! ')
//So, this member can tell some private data of his team.
}
}
}
class01 = new TeamClass('Team A');
class01.add();
class01.add();
class01.members[0].report();
class01.members[1].report();
Also, I have tried to let the nested class can call the upper level's public variable. I have a function like this:
//at upper level class
m.setBelonging(this)
and
//at belonging class
var team = ''
this.setBelonging = function(theOdj){
team = theOdj;
}
(or see it at http://jsfiddle.net/JsJJ9/1/)
Any comment about this approach?
Added at 2014年01月11日 :
Actually, I am trying to ask how can I pass some variable that can't change outside the class (which in my understand, a private variable) to another class. And i found out may be i can do this by nested class. But I don't know is there any defects or there's some better approach.
2 Answers 2
Teams have members, so it almost seems natural to put the Member
class into the Team
class. But projects also have members. How are you going to implement that? You can't nest Member
in both classes. Another problem with nested classes is that you have to traverse through Company -> Department -> Team -> Employee hierarchy just to get everyone's name or email.
Answering your question, how can you pass some variable that can't change outside of the class. Create a getter method:
function Team() {
var teamId = 4; // not changeable from outside
this.getId = function() {
return teamId;
}
}
Now you can access teamId
from outside of the class, but can't change it.
Or instead of creating truly private properties and functions, you can simply prefix them with an underscore to denote they are private and should never be accessed from outside. While nothing really stops you from accessing them, nothing also stops you from redefining the Team
class and accessing anything you want.
Ideally, how you choose to represent the members inside TeamClass
shouldn't matter to the outside world. Your solution is fine, but I think I'd get tired of accessing the members like this:
class01.members[0].report();
It's a bit cumbersome. Plus, why should I care that the members are in some array held by TeamClass
? In fact, do I need to care about MemberClass
at all? Why not further abstract it:
function TeamClass(name) {
var pass = "privateVariable"
var slot = 1;
var members = [];
this.name = name;
this.add = function() {
var m = new MemberClass(slot++);
members.push(m)
}
this.memberReport = function(i) {
if (i >= members.length) {
throw "Invalid member index";
}
members[i].report();
}
function MemberClass(id) {
var id = id;
this.report = function() {
console.log('#' + id + ' : Hello, i got some ' + pass + '! ')
}
}
}
class01 = new TeamClass('Team A');
class01.add();
class01.add();
class01.memberReport(0);
class01.memberReport(1);
Of course, it's hard to tell out of context if this is really the best solution to your problem, but it's something to consider.
-
\$\begingroup\$ I am new to OOP, and i am quiet confused. I'd like to know is it a better practice to put all the function related to "member" into MemberClass ? Because in this example is only 2-levels nested, what if a have a ArmyClass to warp TeamClass ? Also, It's so hard to find information about 'Passing private variable' of javascript , It is a unusual solution that I should avoid ? \$\endgroup\$user3165520– user31655202014年01月11日 08:20:12 +00:00Commented Jan 11, 2014 at 8:20
this.
after a while, at that point you can look into IIFEs. \$\endgroup\$var pass = "privateVariable"
appears to be a placeholder. Could you put the real code in the question so that we can better understand your intention? It's hard to give advice for hypothetical situations. \$\endgroup\$