TL; DR: This is a polyfill for implementing inheritance in versions prior to ES6.
Prior to conversion by the transpiler (most likely it is a TypeScript ), this code (as rightly noted by @Pavel Mayorov ) had the form:
export class DemoAppModel extends observable.Observable { auth() { } } export var mainViewModel = new DemoAppModel();
Let's see what happens here line by line:
// Создаем IIFE(*1), возвращающую конструктор класса DemoAppModel // Аргумент "_super" - конструктор родительского класса. var DemoAppModel = (function (_super) { // Выставляем правильную цепочку прототипов для вновь созданного // конструктора(*2). // Обратите внимание, "DemoAppModel" здесь - это функция определенная // строчкой ниже, а не функция из предыдущей области видимости(*3). __extends(DemoAppModel, _super); // Определяем нашу функцию конструктор. function DemoAppModel() { // Вызываем родительский конструктор в контексте вновь созданного // экземпляра DemoAppModel. Это необходимо для правильной // инициализации переменных экземпляра, определенных через // родительский конструктор. _super.call(this); } // Создаем новый метод для экземпляров DemoAppModel, который ничего // не делает. DemoAppModel.prototype.auth = function () {} // Возвращаем созданный конструктор. return DemoAppModel; // Вызываем IIFE(*1) передавая конструктор родительского класса в качестве // аргумента. })(observable.Observable); // Экспорт функции конструктора и созданного с его помощью экземпляра // для использования в других модулях. exports.DemoAppModel = DemoAppModel; exports.mainViewModel = new DemoAppModel();
Remarks:
- IIEF (Immediately-invoked function expression) is a function that is called as soon as it is defined.
In general, the __extends function has the form (no polyfil for ES <5):
function __extends(ctor, superCtor) { ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, enumerable: false, writable: true, configurable: true } }); };
What exactly happens here is somewhat beyond the scope of the question and requires a separate additional discussion.
In JavaScript, we can use functions up to the moment of their definition due to " Raising Definition ".
Some time ago I told you exactly how prototype inheritance in JavaScript works in this answer . There is a little more detail about the very logic of prototype inheritance implementation.