Javascript 检查产量支持

发布于 2024-08-22 08:45:32 字数 165 浏览 11 评论 0原文

我读到了 JavaScript 中的 Yield 关键字,我需要在我的项目中使用它。我读到这个关键字是从某个版本的 JS 开始实现的,所以我认为旧的浏览器不支持它(对吗?)。

有没有办法检查是否支持yield关键字?或者至少有一种方法可以检查 JS 的版本是否大于或等于实现该关键字的版本(1.7)?

I read about the yield keyword in JavaScript and I need to use it in my project. I read that this keyword has been implemented starting from a certain version of JS so I think that old browsers don't support it (right?).

Is there a way to check if the yield keyword is supported? or at least is there a way to check if the version of JS is greater or equal than the one that implements that keyword (1.7)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

黑寡妇 2024-08-29 08:45:33

这是我对检查本机 yield 支持的看法。

var canYield = (function(){try{yield;}catch(e){}}())!==undefined;

Modernizr 测试

define(['Modernizr'], function( Modernizr ) {
  // native yield support.
  Modernizr.addTest('yield', (function(){try{yield;}catch(e){}}())!==undefined);
});

性能
http://jsperf.com/yield-support
在此处输入图像描述
在此处输入图像描述

Here's my take on checking for native yield support.

var canYield = (function(){try{yield;}catch(e){}}())!==undefined;

Modernizr test

define(['Modernizr'], function( Modernizr ) {
  // native yield support.
  Modernizr.addTest('yield', (function(){try{yield;}catch(e){}}())!==undefined);
});

Performance
http://jsperf.com/yield-support
enter image description here
enter image description here

梨涡少年 2024-08-29 08:45:33

严格来说,只有 Mozilla 浏览器支持 JavaScript。所有浏览器都应该支持 ECMAScript,并且旧版本的 JavaScript 是 ECMAScript 的实现。

此网站列出了哪些版本的浏览器支持哪些版本的 Javascript。

MSIE 使用 JScript。 JScript 中没有yield。因此,使用yield将会限制浏览器对你的页面的支持。

尝试 https://developer.mozilla.org/en/New_in_JavaScript_1.7 了解以下信息使用 JavaScript 1.7

Strictly speaking, only Mozilla Browsers support JavaScript. All browsers should support ECMAScript and older versions of JavaScript are implementations of ECMAScript.

This site lists what versions of Javascript are supported on what versions of browser.

MSIE uses JScript. JScript does not have yield in it. Therefore, using yield will limit the browser support for your page.

Try https://developer.mozilla.org/en/New_in_JavaScript_1.7 for info on using JavaScript 1.7

唯憾梦倾城 2024-08-29 08:45:33

我最近在 yield 上花了很多时间,bobince 并没有完全错误,但 Chrome 31 不会将 JavaScript 版本解释为 1.7 块,即使打开了 Experimental JavaScript 标志(chrome:/ /flags/#enable-javascript-harmony)。由于 Chrome 31 和 Firefox 之间的实现差异,Tymon Sturgeon 的方法无法在启用 Experimental JS 的 Chrome 31 中检测 yield,尽管它非常接近。经过一些修改,它可以在启用 Experimental JS 的情况下检测 Firefox 和 Chrome 31 是否存在 yield

首先,我将快速介绍一下 yield 差异(为了清晰起见,将其写得很长):

在 Firefox 中:

var fooGen = function(){ yield 1; yield 2; };
var iterator = fooGen();
console.log(iterator.next());    // prints 1
console.log(iterator.next());    // prints 2

在启用实验性 JavaScript 的 Chrome 31 中:

// Note the *
var fooGen = function*(){ yield 1; yield 2; };
var iterator = fooGen();
console.log(iterator.next().value);    // prints 1
console.log(iterator.next().value);    // prints 2

Chrome 中需要 .value因为它生成一个对象,但更重要的是,生成器需要在函数定义中使用“*”。另外,我找不到在 Chrome 中从大写“F”函数创建生成器的方法:new Function('', '{yield 5;}');。如果你知道怎么做,请在下面留言。

为了在 Firefox 和 Chrome 中正确检测 yield,我使用了一些来回的代码:

<script type="application/javascript">
    var can_yield = (function(){ 
        try {
            // Assuming Chrome's generator syntax
            var funcUsingYield = new Function('', '{ var interp = function* generator(){ yield true; }}');
            return true;
        } catch(e) { 
            return false; 
        } 
    })();
</script>

<script type="application/javascript;version=1.7">
    // Redefine the `can_yield` function inside a JS1.7 block. Probably safe to simply return true
    can_yield = (function(){ 
        try { 
            return eval("!!Function('yield true;')().next()"); 
        } 
        catch(e) { 
            return false; 
        } 
    })();
</script>

<script type="application/javascript">
    if(!can_yield)
    {
        alert("Can't Yield!");
    }
</script>

测试环境:

  • Firefox 25yield Chrome 31 与实验 JS 开启 配合使用
  • yield
  • Chrome 31 与实验 JS 关闭 配合 使用>:yield 不起作用
  • IE10yield 不起作用

I have spent a lot of time with yield recently, and bobince isn't completely wrong, but Chrome 31 does not interpret JavaScript version to 1.7 blocks, even with the Experimental JavaScript flag turned on (chrome://flags/#enable-javascript-harmony). Due to implementation differences between Chrome 31 and Firefox, Tymon Sturgeon's method is unable to detect yield in Chrome 31 with Experimental JS on, although it is very close. With a few modifications, it can detect the existence of yield for both Firefox and Chrome 31 with Experimental JS on.

First I'll quickly cover the yield differences (writing it the long way for clarity):

In Firefox:

var fooGen = function(){ yield 1; yield 2; };
var iterator = fooGen();
console.log(iterator.next());    // prints 1
console.log(iterator.next());    // prints 2

In Chrome 31 with Experimental JavaScript enabled:

// Note the *
var fooGen = function*(){ yield 1; yield 2; };
var iterator = fooGen();
console.log(iterator.next().value);    // prints 1
console.log(iterator.next().value);    // prints 2

.value is required in Chrome because it yields an object, but more importantly, the generator requires a "*" in the function definition. Also, I could not find a way to create a generator from the capital "F" Function: new Function('', '{yield 5;}'); in Chrome. If you know how, leave a comment below.

To properly detect yield in Firefox and Chrome, I have used a bit of code with some back and forth:

<script type="application/javascript">
    var can_yield = (function(){ 
        try {
            // Assuming Chrome's generator syntax
            var funcUsingYield = new Function('', '{ var interp = function* generator(){ yield true; }}');
            return true;
        } catch(e) { 
            return false; 
        } 
    })();
</script>

<script type="application/javascript;version=1.7">
    // Redefine the `can_yield` function inside a JS1.7 block. Probably safe to simply return true
    can_yield = (function(){ 
        try { 
            return eval("!!Function('yield true;')().next()"); 
        } 
        catch(e) { 
            return false; 
        } 
    })();
</script>

<script type="application/javascript">
    if(!can_yield)
    {
        alert("Can't Yield!");
    }
</script>

Tested in:

  • Firefox 25: yield works
  • Chrome 31 with Experimental JS On: yield works
  • Chrome 31 with Experimental JS Off: yield does not work
  • and IE10: yield does not work
烟燃烟灭 2024-08-29 08:45:33

在注意到 skulpt 也做了类似的事情之后,我们实际上以编程方式在“纯 javascript”中实现了“yield”,即不使用 FIREFOX“yield”,用于睡衣。

理论上,完全相同的事情可以手动完成(即通过遵循如何将函数转换为生成器的规则,费力地手动编码翻译后的函数)或通过 javascript 到 javascript 语言翻译器运行 javascript( !)

实现这样一个“野兽”所需要的是,您必须使该函数能够跳到它最后“退出”的位置(通过yield语句)。因此,所有变量必须存储在临时状态(!),并且代码执行相应地改变,以接受这个临时状态信息。其中包括 while 语句、for 循环以及“if”语句。

可以可以完成......

但这只是一个糟糕的工作:你需要有效地编写一个完整的编译器来解析任何JavaScript程序,然后“转换”它,然后输出修改后的 JavaScript。

we actually implemented "yield", programmatically, in "pure javascript" i.e. WITHOUT USING FIREFOX "yield", for pyjamas, after noting that skulpt also had done likewise.

in theory, the exact same thing could be done either manually (i.e. by laboriously hand-coding a translated function by following the rules on how to turn a function into a generator) or by running javascript through a javascript-to-javascript language translator (!)

what's required to implement such a "Beast" is that you have to make the function capable of skipping up to the point where it was last "exited" (by a yield statement). thus, all variables must be stored in temporary state (!) and the code-execution altered accordingly, to accept this temporary state-info. that includes while statements, for loops as well as "if" statements.

it can be done...

... but it's just a bitch of a job: you need, effectively to write an entire compiler that parses any javascript program, then "transforms" it, and then outputs the modified javascript.

l.

画骨成沙 2024-08-29 08:45:33

呃,实际上有一些技术可以使用,但我怀疑这些技术有任何真正的价值

(function test_key_word(keyword){
var wrong = false;
try {
    eval(keyword + "=42");
} 
catch(e) {
    wrong = true;
} finally {
    if (wrong) { return "definitely wrong" }
    else 
    if (window[keyword] !== 42) {
        return "very probably, wrong"
    }
    else {
        return "can be acceptable, but nevertheless i wouldn't rely upon such tests"
    }
}})("in")

whell, actually there are some techniques you can use, but I doubt such techniques have any real value

(function test_key_word(keyword){
var wrong = false;
try {
    eval(keyword + "=42");
} 
catch(e) {
    wrong = true;
} finally {
    if (wrong) { return "definitely wrong" }
    else 
    if (window[keyword] !== 42) {
        return "very probably, wrong"
    }
    else {
        return "can be acceptable, but nevertheless i wouldn't rely upon such tests"
    }
}})("in")
幸福不弃 2024-08-29 08:45:32

这是一个检查是否可以使用yield 的函数。

var can_yield = (function(){ 
    try { 
        return eval("!!Function('yield true;')().next()"); 
    } 
    catch(e) { 
        return false; 
    } 
})();

Here's a function for checking if yield can be used.

var can_yield = (function(){ 
    try { 
        return eval("!!Function('yield true;')().next()"); 
    } 
    catch(e) { 
        return false; 
    } 
})();
怼怹恏 2024-08-29 08:45:32

yield 为 JavaScript 引入了新语法。除非您通过在 script type 属性 (*) 中包含版本号来指定需要新的 JavaScript 语法,否则您将无法使用 yield

当您指定脚本版本时,只有支持给定版本的浏览器才会执行该块。因此,只有 Firefox,而不是 IE、Opera 或 WebKit,才会执行以下位置的顶部块:

<script type="text/javascript;version=1.7">
    function x() {
        yield 0;
    }
    var canyield= true;
</script>
<script type="text/javascript">
    if (!window.canyield) {
        // do some fallback for other browsers
    }
</script>

(*:请注意,type 属性中指定的类型和版本专门决定是否获取、执行外部脚本,不幸的是,脚本的执行模式被完全忽略。)

yield introduces new syntax to JavaScript. You won't be able to use yield unless you have specified that you want the new JavaScript syntax, by including a version number in the script type attribute(*).

When you specify a script version, only browsers that support the given version will execute the block at all. So only Firefox, and not IE, Opera or WebKit, will execute the top block in:

<script type="text/javascript;version=1.7">
    function x() {
        yield 0;
    }
    var canyield= true;
</script>
<script type="text/javascript">
    if (!window.canyield) {
        // do some fallback for other browsers
    }
</script>

(*: note that the type and version specified in the type attribute exclusively determines whether external scripts are fetched, executed, and the mode for the execution. The Content-Type of a script is, unfortunately, completely ignored.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文