JS function
You can inline JavaScript code into your Kotlin code using the js("…") function. This should be used with extreme care.
fun main() {
js("alert(\"alert from Kotlin!\")") // 1
}
- Sending a JavaScript alert from a Kotlin function.
fun main(){
val json = js("{}") // 1
json.name = "Jane" // 2
json.hobby = "movies"
println(JSON.stringify(json)) // 3
}
- Creates a JavaScript object literal. The
js(...)
function return type isdynamic
. - Adds some properties by utilizing the
dynamic
type capabilities. - Passes the JSON to JavaScript API.