# Builder for dynamic objects?

**URL:** <https://discuss.kotlinlang.org/t/builder-for-dynamic-objects/6077>\
**Category:** JavaScript\
**Created:** [January 4, 2018, 10:53am UTC](https://discuss.kotlinlang.org/t/builder-for-dynamic-objects/6077 "2018-01-04T10:53:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [January 4, 2018, 10:53am UTC](https://discuss.kotlinlang.org/t/builder-for-dynamic-objects/6077/1 "2018-01-04T10:53:19Z")

</div>

I wonder why there is not builder constructor for dynamic objects in kotlin-js standard library.  
I am playing around with calling javascript library from kotlin code and configuration object looks like that:

```kotlin
json(
                "width" to "100%",
                "height" to "400px",

                "controller" to json(
                        "loadData" to { filter: dynamic ->
                            println("loading items with filter: $filter")
                            data.toTypedArray()
                        },
                        "insertItem" to { item: dynamic ->
                            println("inserting item: $item")
                            val point = DataPoint(
                                    phi1 = item.phi1,
                                    psio = item.psio,
                                    psie = item.psie
                            )
                            _data.add(point)
                            point
                        },
                        "updateItem" to { item: dynamic ->
                            println("updating item: $item")
                            item as Any
                        },
                        "deleteItem" to { item: dynamic ->
                            println("removing item: $item")
                            _data.remove(item as DataPoint)
                            item as Any
                        }
                )
}

```

It works fine but looks not good at all. but introducing a very simple function:

```kotlin
fun js(builder: dynamic.() -> Unit): dynamic {
        val obj = Any().asDynamic()
        builder.invoke(obj)
        return obj
}

```

into much cleaner code:

```kotlin
js {
            width = "100%"
            height = "400px"

            controller = js {
                loadData = { filter: dynamic ->
                    println("loading items with filter: $filter")
                    data.toTypedArray()
                }
                insertItem = { item: dynamic ->
                    println("inserting item: $item")
                    val point = DataPoint(
                            phi1 = item.phi1,
                            psio = item.psio,
                            psie = item.psie
                    )
                    _data.add(point)
                    point
                }
                updateItem = { item: dynamic ->
                    println("updating item: $item")
                    item
                }
                deleteItem = { item: dynamic ->
                    println("removing item: $item")
                    _data.remove(item as DataPoint)
                    item
                }
            }
}

```

Is there a reason why it is not done this way? Or maybe it is already defined somewhere and I just have not found it?

---

<div class="post-metadata">

**Author:** ![Anton.Bannykh](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Anton.Bannykh](https://discuss.kotlinlang.org/u/Anton.Bannykh)\
**Post date:** [January 15, 2018, 12:50pm UTC](https://discuss.kotlinlang.org/t/builder-for-dynamic-objects/6077/2 "2018-01-15T12:50:13Z")

</div>

At the moment you wouldn’t be able to create something like `{ "0": "" }`, because

```
js { `0` = ''}

```

doesn’t compile

Otherwise historical reasons I guess.
