In a recent project which uses Kotlin multiple platforms support to generate JavaScript classes used in the front-end, I found out that I need to create instances of these classes at runtime. The typical way is to invoke the constructor function directly with arguments. However, this approach has issues when classes have complicated inheritance hierarchy.

Given following TypeScript code with classes Entity and NamedEntity, it's transpiled to JavaScript code shown below.

class Entity {
    id: string;
    constructor(id: string) { = id;

class NamedEntity extends Entity {
    name: string;
    constructor(id: string, name: string) {
        super(id); = name;
    toString() {
        return `NamedEntity: ${name}`;

Transpiled JavaScript code:

var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
var Entity = /** @class */ (function () {
    function Entity(id) { = id;
    return Entity;
var NamedEntity = /** @class */ (function (_super) {
    __extends(NamedEntity, _super);
    function NamedEntity(id, name) {
        var _this =, id) || this; = name;
        return _this;
    NamedEntity.prototype.toString = function () {
        return "NamedEntity: " + name;
    return NamedEntity;

To create instances of NamedEntity, we usually call new NamedEntity('1', 'abc'). If we only have the reference to the class constructor, we may try to do following to create the instance.

const ctor = window['my-kotlin-module']['NamedEntity'];, '1', 'abc');

The issue here is that we cannot get the correct value of this argument to use when calling the function call. The result object won't have the correct prototype chain configured.

To solve this issue, we can use the Function constructor.

In the code below, we dynamically create a function with body containing the new expression to create new instances and invoke this function with arguments. The trick here is to use string template to generate dynamic function body with desired content. Now we can call createNamedEntity('1', 'abc') to create instances of NamedEntity correctly.

function createNamedEntity(id, name) {
  const ctor = window['my-kotlin-module']['NamedEntity'];
  const func = new Function('ctor', 'id', 'name', `
    return new ctor(id, name);
  return, ctor, id, name);