L
L
lotrop2016-09-01 16:02:10
JavaScript
lotrop, 2016-09-01 16:02:10

Where specifically to read about the correct implementation of OOP in javascript?

While studying books on javascript, I came across different interpretations of how OOP is implemented in this language. Some say do it through prototypes, others copy properties and functions, others call the parent class through Call inside their constructor. How to implement everything correctly?
Plus, the question about the availability of methods and variables is also a lot of discrepancies. The implementation of hidden methods and variables is also not always good, since by returning internal private properties from an object, for example an array, I can already change the value of the array from the outside (it is not clear how to deal with passing by reference, except for returning a copy).
Even in the books, I never saw a sensible implementation of the application architecture. Where can I find specific information on these issues?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
gleendo, 2016-09-01
@lotrop

In ES5 so

// Создание супер класса
var SuperClass = function() {
  // Создание свойств супер класса
  this.prop = "property";
}

// Добавление методов в прототип супер класса
SuperClass.prototype.method = function() {
  return this.prop;
};

// Наследование
var SubClass = function() {
  // Наследование свойств супер класса
  SuperClass.apply(this, arguments);

  // создание собственных свойств
  this.prop = "property";
}

// Наследование методов супер класса
SubClass.prototype = Object.create(SuperClass.prototype);
// Возврат значения свойства constructor
SubClass.prototype.constructor = SubClass;

// Создание собственных методов
SubClass.prototype.subMethod = function() {
  console.log("yo!");
};

// Создания экземпляра класса
var instance = new SubClass();

// Вызов методов экземпляра
instance.subMethod();
console.log(instance.method());
console.log(instance.prop);

F
flatogo, 2016-09-01
@flatogo

good books)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question