有没有办法检查脚本是否在dart vm或dart2js中运行? [英] Is there a way to check if the script is running in the dart vm or dart2js?

查看:189
本文介绍了有没有办法检查脚本是否在dart vm或dart2js中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查脚本是否在dart vm或dart2js中运行?也许使用镜像API?

Is there a way to check if the script is running in the dart vm or dart2js? Maybe using mirrors API?

推荐答案

据我所知,没有官方目的是为了所有的实际目的,你不必知道你是运行原生还是编译为JavaScript。

There is no official way, as far as I know. The intent is that for all practical purposes, you shouldn't have to know if you are running native or compiled to JavaScript.

也就是说,使用。最简单的一个可能是利用Dart有两个数字类型, int double ,而JavaScript只有一个,这相当于Dart的 double ,dart2js没有特殊的实现 int 刚刚。因此,在Dart中, identical(1,1.0) false ,并且VM正确实现, JS,它是 true

That said, there are few hacks you can use. The easiest one is probably to exploit the fact that Dart has two numeric types, int and double, while JavaScript has only one, which is equivalent to Dart's double, and dart2js doesn't have a special implementation of int just yet. Therefore, identical(1, 1.0) is false in Dart, and the VM implements that correctly, but when compiled to JS, it is true.

请注意,您应该在使用之前一个hack这样。在大多数情况下,你不必这样做,只需写Dart,不要尝试识别是否运行JS。此外,任何人都不能保证它将永远工作。

Note that you should think pretty hard before using a hack like this. In most cases, you don't have to do that, just write Dart and don't try to recognize if you are running JS or not. Also, noone can guarantee that it will work forever.

这篇关于有没有办法检查脚本是否在dart vm或dart2js中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆