小编Saf*_*Pin的帖子

如何找出Dart中是否存在变量

在JavaScript中,我可以使用“ in”运算符来检查变量是否存在。因此,也许这段代码可以正常工作。

index.html

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>Using in operator</title>
 </head>
 <body>
  <div id="div1">hello</div>
  <script>
   document.someValue = "testValue";
   if( 'someValue' in document ) {
    document.getElementById('div1').innerHTML = document.someValue;
   }else{
    document.getElementById('div1').innerHTML = "not found";
   }
  </script>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

结果,div1的最终内容将为“ testValue”。但是,Dart没有“ in”运算符。在Dart中,HtmlDocument类确实具有contains()方法。但是,方法的参数类型是Node,而不是String。我也尝试过此代码。

print( js.context['document'] );
print( js.context['document']['someValue'] );
Run Code Online (Sandbox Code Playgroud)

“ js.context ['document']”运行良好,并返回HtmlDocument对象的实例。但是,“ js.context ['document'] ['someValue']”完全不起作用。这不返回任何内容或没有错误。

有什么方法可以检查Dart中变量的存在吗?:-(

感谢您的阅读!

javascript dart

4
推荐指数
2
解决办法
4256
查看次数

标签 统计

dart ×1

javascript ×1