V
V
Vladimir B.2015-06-17 17:27:33
JavaScript
Vladimir B., 2015-06-17 17:27:33

JavaScript: How to correctly implement the ability to "replace/replace" a constructor in a class when it is created and inherited?

Good afternoon.
There is a self-written library for creating and inheriting classes ( code on pastebin ) (the code below shows the creation of a class from a set of parameters / functions) .

var newClass = classConstructor; // Функция - конструктор	

// Проходим по списку функция и прописываем их в создаваемый класс
...

I'm trying to implement something like:
var newClass = function( )
  {
    var callArguments = ( arguments.length > 0 ? arguments : arguments.callee.caller.arguments );
    this[ '___con' ].apply( this, callArguments );
  };
  newClass.prototype.___con = classConstructor;

This is necessary due to the fact that all functions in prototype are subsequently proxied (replaced by aplly call + special function call) , and I want to implement this for the constructor as well.
But for some reason, nothing comes out, but an eternal cycle is obtained at the moments of subsequent inheritance.
What could be the reason?
Or how to do it differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2015-08-13
@ange007

If I understand correctly, then:

function Figure(name) {
    this._name = name;
}
Figure.prototype.getName = function () {
    return this._name;
};

// проксируем
function WrapFigure(name) {
    var obj = Object.create(Figure.prototype);
    Figure.apply(obj, arguments);

    // что-то делаем

    return obj;
}

// проверяем
var cir = new WrapFigure('Circle');
var name = cir.getName();
console.log(name);
// все работает!

But in general
arguments callee
highly discouraged, tk. does not work in 'use strict'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question