JavaScript: external interfaces vs. objects for options


#1

Hi,

I keep tripping up when trying to construct an options-object for passing to a javascript-library.
I want to use classes rather than external interface because they are less verbose and typesafe in construction.
The problem is they often don’t work, because their prototype doesn’t descend from normal new Object() and therefore has no methods like hasOwnProperty.

Any thought on this?

This works

 val options = (js("new Object()") as Any).apply {
    asDynamic().theme = "test"
 }

26

This doesn’t work

class QOption(val theme: String)
val options2 = QOptions(
    theme = "test"
)

44


#2

Wrong. There’s no hasOwnProperty in JavaScript objects. It’s a property of Object (not instances!). I don’t know why they don’t work in your particular case. Where are you going to pass options? What about classes, declared in other languages, like ES6 or TypeScript, do they work normally in your use case?


#3

I’m using the Quill editor. It seems like it is doing something funky with the options-object.

I’m getting

Uncaught TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)

The code where the error occurs:

    var _this = _possibleConstructorReturn(this, 
        (Keyboard.__proto__ || Object.getPrototypeOf(Keyboard))
            .call(this, quill, options)
    );

    _this.bindings = {};

    /* CRASHES */
    Object.keys(_this.options.bindings).forEach(function (name) {
       ...
    });

. I don’t want to waste to much time on this though, I will just use interfaces…


How to set Javascript's function reference in Kotlin?