I am working through “JavaScript Web Applications” an want to append the class library I created. Here is the fiddle http://jsfiddle.net/andrewjmead/gXMp8/. Right now it works, but I remember hearing it is best to avoid using __proto__ in your code. I am currently using this to create functions to manipulate private variables.
Right now the Cat class inherits from the Animal class. I want to add a private variable “name” in the constructor for cat and then create a getName and setName to manipulate this value for each instance.
Var tom = Cat("tom");
tom.setName("fud");
tom.getName(); //fud
Should I be using this.getName() instead of this.proto.getName(), or am I wrong altogether?
I think in dynamic languages like javscript, or Python, there are no needs for really
privatevariables. When we say somethingprivate, we actually say “Don’t touch this stuff if you don’t know what you are doing”. So don’t be too strict about theprivatething. And, if you really want to keep some variablesprivate, I prefer usingclosure. it is a good approach to make some variable reallyprivate. For example:In this approach, there is no way to access the
_namevariable out ofCat. But there are drawbacks you can easily figure out, that you have to write yourgetterandsettermethods inside ofconstructorfunction. The fancyCat.prototype.someFuncdeclaration gone.In my real work, I used to work with javascript for 2 years, I am favor of using the leading
_to tell others, this is a private variable, cause I am a Python fan. 😉