我怎样才能拥有检测ES6发电机? [英] How can I feature-detect ES6 generators?

查看:149
本文介绍了我怎样才能拥有检测ES6发电机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢ES6发电机。有没有一种方法,我可以检测浏览器支持发电机?我知道发电机可能不是很多浏览器(或可能没有浏览器的话)的时刻,但没关系,我的目的。

I'm really enjoying ES6 generators. Is there a way I can detect generator support in browsers? I know generators might not be in a lot of browsers (or possible no browsers at all) at the moment, but that's OK for my purposes.

我试过:

try {
  function *(){}
} catch(err) {
  console.log("No generators");
}

但它似乎并没有工作。

But it doesn't seem to work.

我怎样才能在浏览器中发现了ES6发电机支持?

How can I detect support for ES6 generators in browsers?

推荐答案

其中几次的评估实际上是正确的解决方案。

One of the few times that eval is actually the right solution.

有关语言结构的变化,你需要的东西是这样的:

For language construct changes, you need something like this:

try {
  eval("(function *(){})");
} catch(err) {
  console.log(err);
  console.log("No generators");
}

这篇关于我怎样才能拥有检测ES6发电机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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