我如何await在async函数调用call或apply用Babel?
下面是一个示例,其中getOrders是类的async方法Service:
class Service() {
   async getOrders(arg1, arg2, arg3) {
      return await this.anotherService.getOrders(arg1, arg2, arg3);
   }
}
let service = new Service();
// ...
// Babel doesn't compile 
// let stream = await service.getOrders.call(this, arg1, arg2, arg3);
// producing SyntaxError: Unexpected token for await
let stream = service.getOrders.call(this, arg1, arg2, arg3);
stream.pipe(res); // obviously not working without await in the prev line
更新/解决方案
问题在于 …
我对使用@myDecorator语法的能力感到非常兴奋(使用babel).我试图装饰一个生命周期函数,具体来说componentWillMount,检查装饰器中的组件props和context.但是,我似乎无法访问props或context.我不确定这是否是一种反模式,或者我是否只是出错了.
小例子:
// TestComponent.jsx
import checkProps from 'checkProps.js';
class TestComponent extends React.Component {
    @checkProps
    componentWillMount() {
        // Do something.
    }
    render() {
       return <div>My Component</div>
    } 
}
和
// checkProps.js
export function checkProps(target) {
    console.log(target.props);
}
我也尝试过装饰器和检查的箭头函数this,但我不认为装饰器会以这种方式组合.
我也尝试让我的装饰器成为工厂并传入this.props,this.context但this在装饰组件生命周期功能时未定义.
当我们使用 import() 动态加载模块时,似乎不允许使用模板字符串:
const moduleName = 'myModule'
import(`modules/${moduleName}`) // Error: Cannot find module 'modules/myModule'
与文字字符串的工作原理相同:
import('modules/myModule').then(module => ...)
es6中有没有办法实现模板字符串的动态加载?
// 更新
但这似乎更棘手:
这有效:
import(/* webpackChunkName: "[request]" */ ../pages/${route.componentPath}`)
// componentPath = 'MyModule'
这不会:
import(/* webpackChunkName: "[request]" */ `${route.componentPath}`)
// componentPath = '../pages/MyModule'
我正在使用带有react-router的create-react-app配置。
我怎样才能实现一个装饰器,使一个类的所有方法都可以自动链接?
我有以下课程:
class MyClass {
  async foo() { console.log(1); }
  async bar() { console.log(2); }
}
我希望能够做到以下几点:
@chainableMethods
class MyClass {
  ...
}
const myInstance = MyClass();
myInstance
  .foo()
  .bar();
我正在阅读有关 JavaScript 类的内容,并遇到了“公共类字段语法”这个术语。在深入研究它时,我遇到了这个Babel 关于类属性的文档。
有人可以解释一下 - 在实现方面,这种新语法的用例是什么? (它为 JavaScript 提供了哪些解决方案/好处,哪些是迄今为止缺失的?)
下面是一个例子(在谷歌浏览器中运行没有错误):
class Person {
    firstName = "Mike";
    lastName = "Patel";
    
    // this is a public class field syntax
    getName = () => {
      return this.firstName + " " + this.lastName;
    };
}
var p = new Person();
console.log(p.firstName); // Mike
console.log(p.lastName); // Patel
console.log(p.getName); // () => { return this.firstName + " " + this.lastName; }
console.log(typeof p.getName); // function
console.log(p.getName()); // Mike …除了猜测(就像我在下面所做的那样)之外,是否有一种更直接和有效的方式来反射性地检索您的 JavaScript 环境支持的所有货币的列表?
function getSupportedCurrencies() {
  function $(amount, currency) {
    let locale = 'en-US';
    let options = {
      style: 'currency',
      currency: currency,
      currencyDisplay: "name"
    };
    return Intl.NumberFormat(locale, options).format(amount);
  }
  const getAllPossibleThreeLetterWords = () => {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const arr = [];
    let text = '';
    for (let i = 0; i < chars.length; i++) {
      for (let x = 0; x < chars.length; x++) {
        for (let j = 0; j < chars.length; j++) {
          text …javascript currency internationalization node.js ecmascript-next
是否存在从 Node.js 版本(例如 0.10 或 14)到相应 ECMAScript 版本(例如 ES5、ES2020)的映射?
由于这个问题已经结束,我无法发布我想出的解决方案作为答案,所以它在这里。
@bevry/node-versions包含
fetchESVersionForNodeVersion(nodeVersion: string): Promise<string>它将获取在 Node.js 版本发布时被批准的 ECMAScript 版本。
考虑到有一个应该从TypeScript转换为Babel的项目,文件包含Babel无法忽略的输入信息.
如何以自动方式从整个代码库中删除TS类型的注释和断言?
有没有办法将它们转换为Flow(考虑到某些TS类型的功能可以转换而有些不能)?
显然,这不能通过regexp获得.
该项目在其代码中具有许多TS/ES.next功能,并且应该是可读和可编辑的,因此在es6目标中进行转换不是一个选项.
我试图让es6语法适用于此代码片段.
let checkList = [1, 2].map(i => "Check " + i)
let checks = checkList
  // .reduce((acc, check) => Object.assign(acc, {[check]: {valid: false}}), {})
  .reduce((acc, check) => {...acc, {[check]: {valid: false}}}, {})
console.log(checks)
如果我在https://babeljs.io中使用注释行的输出如下所示,这是我想要使用新语法得到的.
Object {
  "Check 1": Object {
    "valid": false
  },
  "Check 2": Object {
    "valid": false
  }
}
我不确定此代码中是否存在语法错误.我尝试选择babeljs中的所有预设,但它无法正确编译.
如果您指定IANA给定的时区名称,Javascript允许您查看其在另一个时区的时间.例如:
new Date().toLocaleString("en-US", {timeZone: "America/Chicago"});
您可以在下面看到IANA在每个常规时区内提供多个名称:
America/New_York    Eastern (most areas)
America/Detroit Eastern - MI (most areas)
America/Kentucky/Louisville Eastern - KY (Louisville area)
America/Kentucky/Monticello Eastern - KY (Wayne)
America/Indiana/Indianapolis    Eastern - IN (most areas)
America/Indiana/Vincennes   Eastern - IN (Da, Du, K, Mn)
America/Indiana/Winamac Eastern - IN (Pulaski)
America/Indiana/Marengo Eastern - IN (Crawford)
America/Indiana/Petersburg  Eastern - IN (Pike)
America/Indiana/Vevay   Eastern - IN (Switzerland)
America/Chicago Central (most areas)
America/Indiana/Tell_City   Central - IN (Perry)
America/Indiana/Knox    Central - IN (Starke)
America/Menominee   Central - …ecmascript-next ×10
javascript ×10
babeljs ×2
ecmascript-6 ×2
node.js ×2
async-await ×1
class-fields ×1
currency ×1
es6-modules ×1
flowtype ×1
reactjs ×1
timezone ×1
typescript ×1
webpack ×1