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());