飛鏢提供特別註明:
/**
* An annotation used to mark a feature as only being supported by a subset
* of the browsers that Dart supports by default.
*
* If an API is not annotated with [SupportedBrowser] then it is assumed to
* work on all browsers Dart supports.
*/
class SupportedBrowser {
static const String CHROME = "Chrome";
static const String FIREFOX = "Firefox";
static const String IE = "Internet Explorer";
static const String OPERA = "Opera";
static const String SAFARI = "Safari";
/// The name of the browser.
final String browserName;
/// The minimum version of the browser that supports the feature, or null
/// if supported on all versions.
final String minimumVersion;
const SupportedBrowser(this.browserName, [this.minimumVersion]);
}
例如:
@DomName('ApplicationCache')
@SupportedBrowser(SupportedBrowser.CHROME)
@SupportedBrowser(SupportedBrowser.FIREFOX)
@SupportedBrowser(SupportedBrowser.IE, '10')
@SupportedBrowser(SupportedBrowser.OPERA)
@SupportedBrowser(SupportedBrowser.SAFARI)
@Unstable()
class ApplicationCache extends EventTarget {
...
您可以檢測瀏覽器版本,並得到(帶鏡子)一類的註釋代表一些網絡功能。如果它有@Experimental
,並且可能是@Unstable
,那麼即使支持的瀏覽器也不能依賴它。如果它有@SupportedBrowser
註釋和用戶瀏覽器在列表中或根本沒有@SupportedBrowser
那麼你應該沒問題。
我已經試過了。有更好的解決方案嗎? – Robert
在Safari中我得到一個'UnknownJavaScriptObject'。 – Robert
我認爲try-catch是最乾淨的解決方案,假設context.hasProperty()在飛鏢<-> js交換中有更多的開銷。 – marfis