2

Although a property or method may be provided in an Elemental2 overlay, how can you test whether it is actually defined in the underlying JS object?

The DomGlobal class has a hasOwnProperty() method that seems to do what I want:

// WORKS
if (DomGlobal.hasOwnProperty("console")) {
    DomGlobal.console.log(object);
}

But how do I check for the existence of methods on the console object itself? I'd like to be able to do something like this:

// DOESN'T WORK
if (DomGlobal.console.hasOwnProperty("warn")) {
    DomGlobal.console.warn(object);
}

It doesn't work because hasOwnProperty() isn't defined in the console overlay.

What am I supposed to do instead? Do I need to define my own overlay or cast to an overlay that includes the method?

Is this right?

Js.<JsObject>cast(DomGlobal.console).hasOwnProperty("warn")

It seems a bit of a mouthful. Are there standard utilities for doing this more succinctly or do I have to write my own?

4

1 回答 1

4

我在Elemental2的一个旧的已关闭问题中发现了这个答案:

Js.asPropertyMap(object).has("key");
于 2018-09-05T19:45:00.803 回答