# Is there a way to use the "new" operator with arguments on a dynamic variable in Kotlin JavaScript?

**URL:** <https://discuss.kotlinlang.org/t/is-there-a-way-to-use-the-new-operator-with-arguments-on-a-dynamic-variable-in-kotlin-javascript/6126>\
**Category:** Uncategorized\
**Created:** [January 6, 2018, 11:46pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-the-new-operator-with-arguments-on-a-dynamic-variable-in-kotlin-javascript/6126 "2018-01-06T23:46:50Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![snrostov.1](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@snrostov.1](https://discuss.kotlinlang.org/u/snrostov.1)\
**Post date:** [January 7, 2018, 11:56am UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-the-new-operator-with-arguments-on-a-dynamic-variable-in-kotlin-javascript/6126/4 "2018-01-07T11:56:16Z")

</div>

More safe version:

```auto
fun <T : Any> JsClass<T>.createInstance(vararg args: dynamic): T {
  @Suppress("UNUSED_VARIABLE")
  val ctor = this

  @Suppress("UNUSED_VARIABLE")
  val argsArray = (listOf(null) + args).toTypedArray()

  //language=JavaScript 1.6
  return js("new (Function.prototype.bind.apply(ctor, argsArray))").unsafeCast<T>()
}

```

Can be used as

```auto
window.classNameK.jsClass.createInstance(a, b, c)

```

(jsClass is property from std lib)

---

_[View the full topic](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-the-new-operator-with-arguments-on-a-dynamic-variable-in-kotlin-javascript/6126)._
