简体   繁体   English

检查GWT Elemental2叠加层中方法或属性的可用性

[英]Checking availability of a method or property in GWT Elemental2 overlays

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? 尽管可以在Elemental2叠加层中提供属性或方法,但如何测试它是否在基础JS对象中实际定义了?

The DomGlobal class has a hasOwnProperty() method that seems to do what I want: DomGlobal类具有hasOwnProperty()方法,该方法似乎DomGlobal我的要求:

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

But how do I check for the existence of methods on the console object itself? 但是,如何检查console对象本身上是否存在方法? 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. 它不起作用,因为在console叠加层中未定义hasOwnProperty()

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? 是否有标准的实用程序可以更简洁地执行此操作,或者我必须自己编写?

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

Js.asPropertyMap(object).has("key");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM