问题
I am trying to get Firefox to run a Promise in ES6 but run into the 'let' keyword triggering an error;
SyntaxError: let is a reserved identifier
Changing the script tag to include; type="application/javascript;version=1.7" did not work, so I am seeking to Transpile the code.
My situation is that there is nothing being used except a text editor. No NPM, not Node or Angular, no Visual Studio, nothing. So when I investigated the Compilers, I saw no option to let me Transpile this code without any of these other tools/editors/etc.
Is there an option where I do not have to learn, use, install, configure, adapt, another tool and just Transpile it outright, or is there some server-specific reason that this cannot be done? What ARE my options?
Thanks in advance! Hoping someone can school me in ES6 and getting it to work with Firefox so it doesn't trigger the errors and use 'let' the way it is intended to be ran.
回答1:
Yes, You can use ES6 without nodeJs, Follow the below steps
Import babel script
use instead of
Write your ES6 code inside
Cons: You need write all ES6 code in the same HTML page, for example, if index.html is an entry point for the application, then you need to include ES6 code in the index.html page using the script tag of type "text/babel". You can't import the external ES6 file, it will throw cross-origin error.
回答2:
You can write ES6 in the browser with the babel-standalone
<div id="output"></div>
<!-- Load Babel -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Your custom script here -->
<script type="text/babel">
const getMessage = () => "Hello World";
document.getElementById('output').innerHTML = getMessage();
</script>
来源:https://stackoverflow.com/questions/34637104/can-i-transpile-for-es6-es5-without-npm-vs-node-etc-and-just-the-js-code-its