Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

javascript - Using let and yield in Firefox

I'm trying to use let and yield in Firefox. I am testing in both versions 18 and 21 (Nightly) and getting the same results.

Here's my really simple test script:

<html>
    <head>
        <title>test</title>
        <script type="text/javascript">
            'use strict';
            function a() {
                yield 5;
            }
        </script>
    </head>
    <body></body>
</html>

I get this error:

yield is a reserved identifier

Similarly when I do a simple test with let I get "let is a reserved identifier", which is really frustrating because let has supposedly existed in Firefox since version 2!

Strangely, if I execute the same code in Firebug it works!

I have tried various other strings in the type and language attributes of the script tag but have not found a magic one that works.

What's going on? How do I get this stuff working with a script tag?


Edit

Hmm, I see, so you must specify the version number. I had tried this, but for my original more complicated script which used web workers. Apparently using version=1.7 on a script which includes a web worker which includes a script that uses let and yield isn't good enough -- the web worker script still breaks... Then I tried reducing to simplest case but apparently didn't try version=1.7 in simplest case.

Thanks... Might post another question in a little bit (after searching) on how to get this working for web workers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mdn note said,

The yield keyword is only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version)

So changing <script type="text/javascript"> to <script type="application/javascript;version=1.7"> will make it work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...