2015版ECMAScript规范,现在是标准(ECMAScript 2015)。仅使用此标记,其中的问题与ECMAScript 2015中提供的新功能或技术更改有关。
我的增减数量函数有问题。增加数量效果很好,但减少数量就会破坏它。例如,我单击加号 19 次,然后单击减号...
JS:在客户端启用导出/导入(ES6 或使用 babel)?
我想导出/导入应用程序目录中的本地文件: 我的index.html: <p>我想导出/导入应用程序目录中的本地文件:</p> <p>我的<pre><code>index.html</code></pre>:</p> <pre><code><!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" data-cfemail="eb898a898e87c6989f8a858f8a8784858eabddc5dadec5db">[email protected]</a>/babel.min.js"></script> </head> <body> <script type="text/babel" src="main.js"></script> </body> </html> </code></pre> <p><pre><code>actions.js</code></pre>:</p> <pre><code>export const ADD_TODO = 'ADD_TODO' export const TOGGLE_TODO = 'TOGGLE_TODO' export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER' </code></pre> <p><pre><code>main.js</code></pre>(从index.html加载):</p> <pre><code>import {ADD_TODO, TOGGLE_TODO, SET_VISIBILITY_FILTER} from 'actions' </code></pre> <p>现在如果我使用没有 babel 的 javascript,我会得到:</p> <p><pre><code>Uncaught SyntaxError: Unexpected token import</code></pre></p> <p>我使用<strong>Chrome浏览器版本60</strong>。这个版本不是应该已经支持ES6了吗?通过支持,我应该能够使用导出/导入?</p> <p>我还尝试了 babel (使用从 <pre><code>index.html</code></pre> 加载的独立 babel)。</p> <p>然后我收到此错误:</p> <pre><code>Uncaught ReferenceError: require is not defined at <anonymous>:4:16 at n (https://unpkg.com/<a href="/cdn-cgi/l/email-protection" data-cfemail="61030003040d4c1215000f05000d0e0f0421574f50544f51">[email protected]</a>/babel.min.js:12:27049) at r (https://unpkg.com/<a href="/cdn-cgi/l/email-protection" data-cfemail="b7d5d6d5d2db9ac4c3d6d9d3d6dbd8d9d2f7819986829987">[email protected]</a>/babel.min.js:12:27558) at e.src.i.(anonymous function).error (https://unpkg.com/<a href="/cdn-cgi/l/email-protection" data-cfemail="33515251565f1e4047525d57525f5c5d5673051d02061d03">[email protected]</a>/babel.min.js:12:27873) at XMLHttpRequest.i.onreadystatechange (https://unpkg.com/<a href="/cdn-cgi/l/email-protection" data-cfemail="57353635323b7a2423363933363b38393217617966627967">[email protected]</a>/babel.min.js:12:27316) </code></pre> <p>我知道客户端不存在<pre><code>require</code></pre>,但这不是 ES6 的导出/导入(不是 NodeJS 导出)语法吗?..</p> <p>我是否需要求助于<pre><code>webpack</code></pre>之类的东西才能发挥作用?</p> <p>根据这个答案:<a href="https://stackoverflow.com/questions/35496479/trying-es6-imports-with-chrome-but-it-doesnt-seem-to-work">尝试使用 Chrome 导入 ES6,但它似乎不起作用</a>您需要在 Chrome 中启用 <pre><code>Experimental Web Platform</code></pre> 标志并使用 <pre><code><script type="module" src="main.js"></script></code></pre>。但使用它会停止加载任何内容。看来浏览器只是忽略了这种类型。即使这种方法可行,那么我想我将无法使用 babel,因为它会使用不同的类型?</p> <p>附注根据这个:<a href="http://2ality.com/2014/09/es6-modules-final.html#named-exports-several-per-module" rel="noreferrer">http://2ality.com/2014/09/es6-modules-final.html#named-exports-several-per-module</a>它应该可以工作..</p> </question> <answer tick="false" vote="4"> <p><pre><code>import</code></pre> / <pre><code>export</code></pre> 语句现在受 <a href="https://caniuse.com/#search=javascript%20modules" rel="nofollow noreferrer">97+% 的所有浏览器支持</a></p> <h1>如何使用</h1> <p>在模块(myModule.js)中:</p> <pre><code>const myVar = 'Hello !!'; export { myVar } </code></pre> <p>在index.html中</p> <pre><code><script type="module"> import myVar from './myModule.js' console.log(myVar) // output 10 </script> </code></pre> <p><a href="https://jakearchibald.com/2017/es-modules-in-browsers/" rel="nofollow noreferrer">这是一篇关于如何使用它的好文章</a></p> </answer> <answer tick="true" vote="2"> <p>Chrome已经实现了大部分es6新功能,除了<pre><code>import / export</code></pre>尚未实现,更多详情:<a href="https://ruanyf.github.io/es-checker/" rel="nofollow noreferrer">https://ruanyf.github.io/es-checker/</a></p> <p>如果你不想使用webpack来编译文件,可以尝试:</p> <pre><code>$ npm install --global babel-cli </code></pre> <p>然后:</p> <pre><code>babel example.js -o compiled.js </code></pre> <p>最后你会得到编译好的文件,该文件将支持浏览器。</p> <p>加上,关键字<pre><code>require/exports/module.exports</code></pre>是CommonJS规范,由Node.js支持。文件<pre><code>https://unpkg.com/<a href="/cdn-cgi/l/email-protection" data-cfemail="53313231363f7e2027323d37323f3c3d3613657d62667d63">[email protected]</a>/babel.min.js</code></pre>使用了CommonJS规范,所以在浏览器端会报错</p> </answer> </body></html>
在nodejs中的构造函数内部析构对象以将值分配给成员变量不起作用
我正在尝试在构造函数内进行 ES6 对象解构,希望为成员变量赋值。它不起作用。当我在时它显示未定义 打印成员函数内的值...
是否有一种简洁的 es6 功能方法可以按类型对项目进行分组,因为它是不可变的? 账户.json [ {"account": {"name": "Bob 的信用", "type": "信用", "id": "1"}}, {“根据...
我想使用对象文字表示法来生成命名空间: const featureGoogleAnalytics = { gTagId: 'G-XXXXXXX', // 您的 Google Analytics ID ... resetConsent: () => { // eslint-disable...
我正在使用 Jest 为 Node.js 后端开发一些测试,我需要检查来自第三方的一些值。在某些情况下,这些值可以是布尔值或空值。 现在...
我的问题是我需要用户确认是否要继续关闭或刷新页面。如果他按“否”,则不会关闭也不会刷新页面。 请看一下我到目前为止的代码: 使用Eff...
根据ES6速记初始化程序,以下两种方法是相同的: 在 ES5 中 var 人 = { 名称:“人”, 问候:函数(){ 返回“你好”+this.name; } }; 在 ES6 中 var 人 = ...
我正在做一个电子商务项目。 正常价格为 549 美元 12.96% 已关闭 如果我从 549 美元减去 12.96% 销售价格变为 477.8496 美元 正如下面的屏幕截图所示。 我想显示接近的促销价...
Object.hasOwnProperty() 产生 ESLint 'no-prototype-builtins' 错误:如何修复?
我使用以下逻辑来获取给定键的 i18n 字符串。 导出函数 i18n(key) { if (entries.hasOwnProperty(key)) { 返回条目[键]; } else if (typeof (Canadarm) !== '
疑问: 根据 MDN 文档,then() 和 catch() 立即返回一个 Pending Promise,如果处理函数没有返回任何内容,则返回的 Promise 会以未定义的 va 形式实现...
then() 和 catch() 返回 Promises,指向内存中的同一个实例
疑问: 根据 MDN 文档,then() 和 catch() 立即返回一个 Pending Promise,如果处理函数没有返回任何内容,则返回的 Promise 会以未定义的 va 形式实现...
所以我在存根构造函数方面遇到了一些问题,更重要的是继承类构造函数...... 我将从一些示例代码片段开始: 父.js module.exports = 类父级 { 构造...
我想将arr2的元素推入arr1,但它不应该重复这些元素 让 arr = [{obj: "ABC"},{obj: "XYZ"},{obj: "LMN"}] const arr2 = [{obj: "ABC...
在 JavaScript 中实现 Promise Chaining 时出现意外输出
我有一个任务,如下所示,我必须在 JavaScript 中使用 Promise Chaining 来解决该任务,并且我得到了一些意外的输出 -: 任务: 如果迈克能够打扫房间,那么他就会去踢足球。 在
如何处理 Array.prototype.reduce() 函数中的 eslint no-param-reassign 规则
我最近添加了 eslint 规则 no-param-reassign。 然而,当我使用reduce构建一个对象(空对象作为initialValue)时,我发现自己需要修改累加器(
我在node.js应用程序中使用Javascript ES6功能: 类pairKey { 构造函数(x_pos,y_pos){ this._X = x_pos; this._Y = y_pos; } 得到 x() { 返回这个._X; } 设置 x(x_pos) {...
importmap 和 module 无法在带有 Spring Boot 的普通 JavaScript 中工作
为什么 importmap 不起作用,为什么尽管指定了 type='module',但导出和导入在普通 JavaScript 文件中根本不起作用?这是在三种现代浏览器中进行测试的。脚本可以...
在CustomDecorator中,如何访问Nest.js中定义的服务实例? 导出 const CustomDecorator = (): MethodDecorator => { 返回 ( 目标:对象, propertyKey: 字符串 |符号,...
我遇到了这个问题,无法将 Jest 与 ES6 一起使用,但它在 CommonJS 中对我有用! 如果我使用标准的 Common JS require,我可以使用带有 jest 的手动模拟,这样这个目录就会被看到 __mocks__ 但 ES6 不是