I think you are trying to use “object” in a way that JS uses an “object”. It’s the same word but they mean totally different things.
Here’s a class that stores any number of greetings. It adds items in a map (key-value data structure):
class GreetingTranslator {
val translations: MutableMap<String, String> = mutableMapOf()
}
fun main() {
val myGreetings = GreetingTranslator()
myGreetings.translations += Pair("Spanish", "Hola")
myGreetings.translations += "Italian" to "Salve" // Another way to add a key-value pair. Same thing as Pair("one", "two")
println(myGreetings.translations)
}
In JS, the idea of an “object” is more like a container or dictionary where you can look up properties and functions with a name. This is very different from how the word “object” is used in other languages. Some of the designs you may be used to from JS likely rely on using a Map
and adding/removing from it. You’ll find that in Kotlin you still can do those patterns even though they’ll look slightly different. You’ll also discover that there are alternative designs that you can’t do well in JS that are available in Kotlin.