2

I want to use Kotlin to generate some JavaScript like following:

function MyComponent() {
    self.constructor = function() {}
}

The problem is constructor is a keyword in Kotlin, I can't just write like:

class MyComponent {
  fun constructor() {}
}

I also tried:

class MyComponent {
    @JsName("constructor")
    fun ctor() {}
}

It still report errors like:

JavaScript name generated for this declaration clashes 
with built-in declaration {1}

How to generate a javascript function which has name constructor?

5
  • I know it's not really solving your problem, but maybe just try changing the letter 'c' to uppercase? ... Just a suggestion... Commented Apr 3, 2018 at 14:52
  • @JO3-W3B-D3V The constructor name is required by the js lib used with kotlin, can't be changed Commented Apr 3, 2018 at 14:59
  • I see your problem, I've never used kotlin personally, I've been meaning to, however, I've never actually used it, JS on the other hand... have you tried something like window["constructor"] within your JS? I'm not sure if that would work or not... Commented Apr 3, 2018 at 15:04
  • 1
    @JO3-W3B-D3V thanks, this is indeed a method worth trying Commented Apr 3, 2018 at 15:16
  • No problem at all, I hope it works fine for you. I think it may work due tot he way it's been written, I may be wrong, who knows. Let me know if it works or not? :) Commented Apr 3, 2018 at 15:20

1 Answer 1

3

There should be no problem with the top-level functions. The fun constructor() {} should just work, yielding function constructor(){}. At least that's what it does in Kotlin 1.2.31.

On the other hand member functions named constructor are prohibited (e.g. you cannot get A.prototype.constructor = function () {} in the output js file). For one thing that would ruin the is-check implementation.

Modifying the constructor property inside the class constructor should be possible:

// Kotlin
class A {
  init{
    this.asDynamic().constructor = fun(a: Int) { println(a) }
  }
}

// JS
function A() {
  this.constructor = A_init$lambda;
}
function A_init$lambda(a) {
  println(a);
}

Hope that helped.

Sign up to request clarification or add additional context in comments.

6 Comments

You say it should just work? - This isn't really useful feedback? I personally don't use Kotlin myself, but as I said, could he not just do something like window["constructor"]? Again, I wouldn't know, I know JS, Kotlin, not so much.
@anton Yes, you are right if fun constructor is at top level, thanks! Actually I simplified my problem too hard, my real problem is the constructor inside a class. Let me modify the question
@Freewind It is possible to modify the property inside the class constructor (added to the answer). Is that what you want?
@JO3-W3B-D3V I was being polite. No, window["constructor"] is not the solution, because a) it solves the problem that doesn't exist, and b) window is not defined in some environments (e.g. NodeJs)
@Freewind by the way, could you tell what exactly that library uses the constructor property for?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.