Private variables in Javascript classes
So, in most classical languages, you can declare a private variable in a class. No big deal. You can declare a private function too. Private is good as well all know. You can't really have encapsulation without private/protected variables. Encapsulation is maybe the most important part of Classical Object Oriented Design.
In javascript, you can create a private variable in a constructor as a local var like this:
Person = function(age) {
var _age = age; this.getAge = function() { return _age; } }; var p = new Person(4); p._age; // undefined p.getAge(); // 4
Cool! I have encapsulation, but one thing we know about javascript is that functions declared withing the constructor are duplicated for each object. That's not good. So if I create another person, it has its own getAge function!
var q = new Person(5);
Protoype functions solve the problem of function duplication among many objects. BUT only functions that are declared in the same scope as the local variable will have access to that variable (via a closure). So prototype functions cannot access private variables.
So I got to thinking. Coffeescript implements classes. How does it implement private variables? Answer. It does not . You put an underscore in front of variables and functions you want to "act" as private and then hope that developers are smart enough not to access them directly.
But there is a way to implement private functions! See this answer on stack overflow. http://stackoverflow.com/a/8334365/1804678.
The comments are interesting too. It is impossible to unit test private functions, but then maybe you just need to implement a public wrapper around the private one.
Reading this post at evanhahn.com,the private variable is not an instance variable, but a class level private variable. That is almost never what we are looking for unless we are implementing a singleton. He comes up with a pretty reasonable conclusion. Don't implement private variables and functions. It's not worth the trouble. I am a little let-down by that, but I guess we just move on and keep it simple.
In javascript, you can create a private variable in a constructor as a local var like this:
Person = function(age) {
var _age = age; this.getAge = function() { return _age; } }; var p = new Person(4); p._age; // undefined p.getAge(); // 4
Cool! I have encapsulation, but one thing we know about javascript is that functions declared withing the constructor are duplicated for each object. That's not good. So if I create another person, it has its own getAge function!
var q = new Person(5);
Protoype functions solve the problem of function duplication among many objects. BUT only functions that are declared in the same scope as the local variable will have access to that variable (via a closure). So prototype functions cannot access private variables.
So I got to thinking. Coffeescript implements classes. How does it implement private variables? Answer. It does not . You put an underscore in front of variables and functions you want to "act" as private and then hope that developers are smart enough not to access them directly.
But there is a way to implement private functions! See this answer on stack overflow. http://stackoverflow.com/a/8334365/1804678.
The comments are interesting too. It is impossible to unit test private functions, but then maybe you just need to implement a public wrapper around the private one.
Reading this post at evanhahn.com,the private variable is not an instance variable, but a class level private variable. That is almost never what we are looking for unless we are implementing a singleton. He comes up with a pretty reasonable conclusion. Don't implement private variables and functions. It's not worth the trouble. I am a little let-down by that, but I guess we just move on and keep it simple.
Comments
Post a Comment