Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

581
Views
How to define a global js function in Kotlin?

Every function and variable that I create in KotlinJs project gets into a module. But I need to define some functions in global scope.

I use p5js library (pure js). It allows user to define event handling functions in global scope. I'm trying to use KotlinJS in this project. But I don't know how to create global functions to handle p5js's events. All my Kotlin functions are inside of the module. And to call my Kotlin code I need to specif the full name mymodule.draw()

Currently I have to make an additional layer of pure JS code with global funcs that translate execution to kotlin functions which looks like this:

function setup() {
    mymodule.setup();
}

function draw() {
    mymodule.draw();
}

The problem with this approach is a lot of boilerplate and repetitive code.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In case this will be useful for somebody I will leave another workaround here:

import kotlin.browser.window

fun main() {
    window.asDynamic()["setup"] = ::setup
    window.asDynamic()["draw"] = ::draw
}

fun setup() {}
fun draw() {}

What it actually does, it creates functions in kotlin module as usual and then assigns them to window object, which makes it global.

This solution is still not ideal, cause it needs a manual assignment for every function. At least it does it right in Kotlin project, no need to maintain a separate pure js file. Maybe it is possible to create an annotation and leverage kotlin reflection(no idea how it's supported in KotlinJS).

Although this solution works for me, I would like to have some out of the box solution like they do for @JsNonModule external functions.

over 4 years ago · Santiago Trujillo Report

0

Unfortunately there is no way to define global function in Kotlin/JS. It is possible to use Plain module type where you have global symbols in module object which is defined in global scope.

// module M
fun foo() {}

which is accessible via M.foo

over 4 years ago · Santiago Trujillo Report

0

Adding on top of @Sergey's Answer, one can also use this work around when dealing with libs like p5.js

fun main() {
    window.asDynamic().setup = {
        // your setup code here
    }

    window.asDynamic().draw = {
        // your draw code here
    }
}

This approach minimizes the definition and the declaration of the two functions (looking at you C Language). Thanks

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!