jjzjj

es6-modules

全部标签

javascript - 使用 Babel.js 将 ES6 箭头函数编译为 Es5

在Mozilla文档中查看ES6箭头函数的文档时,我了解到箭头函数应用严格模式的所有规则,除了在link中描述的规则。varf=()=>{'usestrict';returnthis};varg=function(){'usestrict';returnthis;}console.log(f());//printsWindowconsole.log(g());//printsundefined//wecantestthisinfirefox!但是,Babel.js将箭头函数代码转换为ES5代码,返回undefined而不是Window(demolink)"usestrict";setT

javascript - Node module.exports 返回未定义

我遇到了Node.js和module.exports的问题。我知道module.exports是一个返回对象的调用,该对象具有分配给它的任何属性。如果我有这样的文件结构://formatting.jsfunctionFormat(text){this.text=text;}module.exports=Format;用这个://index.jsvarformatting=require('./formatting');有没有办法初始化一个Format对象并像这样使用它?formatting('foo');console.log(formatting.text);每当我尝试这样做时,我都

javascript - ES6 用箭头函数给变量赋值

我刚刚开始习惯ES6语法,我想知道是否可以使用箭头函数为变量赋值。我正在编写一个基本的轻量级AJAX帮助程序库,并且在状态为200时,我想向用户返回一个有效负载,我目前正在这样做:varresponseData="";switch(payload.returnType.toLowerCase()){case"json":responseData=JSON.parse(httpRequest.responseText);break;case"text":responseData=httpRequest.responseText;break;default:responseData=nul

javascript - 判断回调函数的 是否是 ES6 箭头

这个问题在这里已经有了答案:JavaScriptES6:Testforarrowfunction,built-infunction,regularfunction?(10个答案)关闭6年前。由于普通箭头函数和ES6箭头函数之间的上下文差异很大,我希望能够找出回调fn接收到的是哪一个。typeof将return两者的function。有什么办法区分吗?

javascript - ES2015+ Nested Rest 解释

我正在查看node.green在destructuring,assignment>nestedrest下,使用了以下示例函数:functionf(){vara=[1,2,3],first,last;[first,...[a[2],last]]=a;returnfirst===1&&last===3&&(a+"")==="1,2,2";}console.log(f())现在,我理解了解构,但我不明白为什么a被重写为[1,2,2]同时[...[a[2],last]]=a;返回[1,2,1] 最佳答案 [first,a[2],last]=

javascript - typescript + moment.js : error TS2307: Cannot find module 'moment'

我正在开发一个网络应用程序,使用angular1.5、typescript2.4.0、moment:2.18.1和gulp进行项目组装。这是我的tsconfig.json:{"files":["src/app/main.ts","types/**/*.ts"],"compilerOptions":{"noImplicitAny":false,"target":"es2015","allowSyntheticDefaultImports":true}}在我的date-range-picker.component.ts中。我正在导入时刻库,正如maindocumentation中所建议的那

javascript - Babel ES6 导入错误,SyntaxError : Unexpected token import

我正在尝试设置一个基本的模块化程序,但我似乎遇到了导入模块的问题。我尝试导入我的自定义模块,但出现以下错误:(function(exports,require,module,__filename,__dirname){importtestStepfrom'testStep';^^^^^^SyntaxError:Unexpectedtokenimport导致问题的代码:测试用例.jsimporttestStepfrom'testStep';testStep.hello();测试步骤.jsvartestStep={hello:hello,};varhello=()=>{console.lo

javascript - ES6 文件上的谷歌地图回调

我正在尝试在ES6文件上添加回调,但找不到它。我收到此错误消息:“initMap不是函数”我的文件是这样的:&callback=initMap">我的js文件是:exportfunctioninitMap(){map=newgoogle.maps.Map(document.getElementById('map'),{center:{lat:-34.397,lng:150.644},zoom:8});fetch('/data/markers.json').then(function(response){returnresponse.json()}).then(plotMarkers);

javascript - 带有 es5 的 Angular 5

Angular4支持以下语法varHelloComponent=ng.coreComponent({selector:'hello-cmp',template:'HelloWorld!',viewProviders:[Service].Class({constructor:[Service,function(service){},`});在Angular5中,缺少类,目前任何人都可以为Angular5提供ES5语法我无法切换ES6,所以请避免使用该建议。如果切换到ES6是唯一的方法,那么我现在将坚持使用Angular4 最佳答案 您

javascript - 使用 Jasmine 或任何其他替代方案在 Node 上运行测试 .mjs/ESM

由于--experimental-modulesCLI开关(即node--experimental-modules),我的基于Node的项目是在Node上使用原生ES模块支持实现的.显然,当我使用Jasminenode--experimental-modules./node_modules/jasmine/bin/jasmine运行规范时,出现以下错误:Error[ERR_REQUIRE_ESM]:MustuseimporttoloadESModule是否可以在Node中通过ES模块使用Jasmine?如果不是,是否有任何替代方法不使用框架(例如,使用npm脚本运行测试)?