Как я могу вызвать конструктора JavaScript, использующего вызов, или подать заявку? [дубликат]

82
задан ROMANIA_engineer 22 July 2017 в 22:22
поделиться

1 ответ

Попробуйте следующее:

function conthunktor(Constructor) {
    var args = Array.prototype.slice.call(arguments, 1);
    return function() {

         var Temp = function(){}, // temporary constructor
             inst, ret; // other vars

         // Give the Temp constructor the Constructor's prototype
         Temp.prototype = Constructor.prototype;

         // Create a new instance
         inst = new Temp;

         // Call the original Constructor with the temp
         // instance as its context (i.e. its 'this' value)
         ret = Constructor.apply(inst, args);

         // If an object has been returned then return it otherwise
         // return the original instance.
         // (consistent with behaviour of the new operator)
         return Object(ret) === ret ? ret : inst;

    }
}
50
ответ дан 24 November 2019 в 09:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: