Hello. How to extend the functionality of the method (function). For example, there is a certain class (function)

MyFunction = function() { this.myMethod = function() { //некие вычисления } } 

How can I make sure that when the myMethod function is myMethod , the commands that are executed inside it, plus a few more? It is also necessary that everything inside the function (class) MyFunction remains unchanged (that is, not to edit the code inside MyFunction ).

UPD1 :
If in other words to describe:
There are about fifty classes. In each of these classes there is a method called initData , which performs each of its operations. All these classes have the same superclass (all fifty classes extend the functionality of a single class (function)). It took me to all classes, to each method, to add several more identical operations. Is it possible? If so, how?

UPD2 :
There is one class

 MySuperclass = function() { //некоторые методы } 

There are a number of subclasses of this type:

 MySubclass1 = function() { //Некоторые методы, переопределяющие родительские //Плюс метод initData: this.initData = function() { //Содержание метода /* Основная проблема состоит в том, что я тут не указывал конструкцию вида MySubclass1.superclass.initData.apply(this, arguments); */ } } // 

I need to override all initData methods in subclasses. If it is still unclear what should be done, ask.
UPD3 :
All subclasses inherit their superclass. how this happens, I dropped because of uselessness.

  • Describe the task normally, as I understand it now - this is impossible and is a duristika - Zowie
  • Updated the question. - Anton Mukhin
  • one
    I do not understand your idea. - Anton Mukhin
  • those. function class1 () {} class1.prototype.initData = function () {} function class2 () {} class2.prototype.initData = function () {// should I call the parent initData here? ..} If not, then I am not I understand what you want - Zowie
  • something unreal to a campaign ... - Palmervan

1 answer 1

The answer is simple! You just need to put a method override in the superclass constructor:

 this.constructor = function() { //console.log('constructor: function() {'); var ld = this.initData; this.initData= function() { //Дополнительные методы ld.apply(this, arguments); }; //MySuperclass.superclass.constructor.call(this, arguments); }