The following is an example of JavaScript inheritance: Objects are created - the parent Animal with the variable can_walk and name , and a descendant of Human with the name 'man' . There is also a 'static' counter 'counter' in Animal . How to make 'counter' private (like in Java). Where exactly and how does 'counter' need to be declared?

 function Animal(name) {//name - private //can_walk - public this.can_walk = true; //getName() - public this.getName = function() { return name; } } //getCounter() - public static Animal.prototype.getCounter = function() {return counter;} //setCounter() - public static Animal.prototype.setCounter = function() {counter++;} function Human() { //Декларируем, инициализируем свойства родителя для потомка Human.superclass.constructor.apply(this, arguments); } //Функция, реализующая наследование через прототипы function extend(Child, Parent) { var F = function (){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.superclass = Parent.prototype; } //Запускаем наследование extend(Human, Animal); //Проверяем наследование var animal = new Animal('animal'); var human = new Human('man'); console.log(animal.can_walk); console.log(human.getName()); 
  • there are no access modifiers in javascript - Zowie
  • I understand it. But this is not quite so ... For example, Animal has a name property, and a getName function, which is equivalent to private in Java, since there is no open access to this variable .. - almac
  • I bet you do not write on Javascript such code where I could not get access to your "private" field directly? - Zowie
  • I do not argue :))) I just started to learn the language and I am not yet familiar with it. If simple, please give an example of how you can access the name variable in Animal. - almac Nov.

3 answers 3

So, to write counter as a static variable, you need to initialize the variable:

 Animal.counter = 0; Animal.getCounter = function {return Animal.counter;} Animal.setCounter = function (param) {Animal.counter = param;} 

You can add the variable "this.counter = 0" to the Animal class and check that the static variable is "Animal.counter! == this.counter".

Everything is well described in the article: http://karaboz.ru/?p=9

The answer to the question is: if a static variable can be made private, then for this you need to use complex programming patterns (patterns).

     function Animal(name) {//name - private //can_walk - public this.can_walk = true; var counter; //не будет доступна извне //getName() - public this.getName = function() { return name; } } 
    • Look here javascript.ru/tutorial/object/inheritance#private-chleny , is not very badly written. - Mavrin
    • That is, if you want to make private methods, you need to revise the function declarations in the prototype. And choose the inheritance method of factories. - Mavrin 2:57
    • @almac, you are confused with the notion of static) <br> static is a class method, not its instances <br> PS: Common instance method: Animal.prototype.getCounter = function () {return counter;} Class method (static) : Animal.getCounter = function () {return counter;} General property of instances: Animal.prototype.shared_var = {} - timka_s
    • As I understand it, if Animal.prototype.getCounter is not copied anywhere, and this function is contained only in the singular (there are no copies for it), then this is a static method. And Animal.getCounter will be inherited and there will be a copy of this method in each descendant, so this is no longer static (you need to look in the framework of the example). But Animal.prototype.shared_var is a static property, but at the same time public (i.e., public property). I need the same, but private static. I switched from Java programming to Javascript, and I see that there is too much confusion in JS among programmers ... - almac
    • In JS, statics is not something that is not copied, but something that does not use (this) and local variables of the constructor - timka_s

    You can use a closure to close a variable

     var Animal = function(){ var counter function Animal(name) {//name - private //can_walk - public this.can_walk = true; //getName() - public this.getName = function() { return name; } } //getCounter() - public static Animal.getCounter = function() {return counter;} //setCounter() - public static Animal.setCounter = function() {counter++;} }();