Why do we need private variables?
What are they for and how do we make it开发者_运维知识库? Can you give me an example?
To avoid collisions with multiple libraries, for example.
Say they both use a variable commonly used such as data
. If both libraries use private variables it's all fine:
var lib1 = (function() {
var data;
return {
get: function() { return data },
set: function(v) { data = v }
};
})();
// Supposed to do something different:
var lib2 = (function() {
var data;
return {
get: function() { return data },
set: function(v) { data = v }
};
})();
lib1.set(123);
lib2.set(456);
lib1.get(); // 123
lib2.get(); // 456
However suppose they don't use private variables but global ones like this:
var lib1 = (function() {
return {
get: function() { return data },
set: function(v) { data = v }
};
})();
// Supposed to do something different:
var lib2 = (function() {
return {
get: function() { return data },
set: function(v) { data = v }
};
})();
lib1.set(123);
lib2.set(456);
lib1.get(); // 456 - overwritten by lib2. lib1 might not work properly anymore.
lib2.get(); // 456
So lib1.get()
will fetch the same data as lib2.get()
.
This example is too obvious of course but to stay safe it's a good practice to use private variables.
Variables are encapsulated within a class to stop their names colliding. These can be public or private. Sometimes there is a need to make sure that variables are only changed using the functions that set them. For example the parts of a date would need to be verified to stop someone setting an invalid date such aas February 45th.
var factorial = (function(){
var precog = [1,1];// ===undefined for other indices, N = undefined || N
return function(y){
return precog[y] || (precog[y]=y*arguments.callee(y-1));
};
})();
Here is a JavaScript function with a private precog. This stores previously calculated values and it is private to stop them being manipulated.
I believe there are multiple reasons for namespaces. One way I understand it is: private
, protected
, and public
expressions help a lot in team environments so that other devs don't end up using methods you didn't intend them to. That being said, you only use private
when methods or properties only need to be accessed by that same object. Use protected
when you need an inheriting class to carry that same functionality and use public
when an object of a different class needs to access your object.
eg:
Class Victim
has
- private method haveAnxiety()
- public property appearsToBeRich:Boolean
Class Robber
has
- private method profile(obj:Victim)
- private method rob(obj:Victim)
Robber->profile()
will need to access Victim->appearsToBeRich
. If Victim->appearsToBeRich
returns true
, then that Victim
object will get robbed. a Robber
object never needs to run Victim->haveAnxiety
as the Victim
will run this->haveAnxiety()
as soon as that Victim
object starts getting robbed.
Granted, real world examples are a LOT more complex than my example (and I hope they are much more graceful). Anyway, I hope that helps.
精彩评论