ES6模块:导出单类静态方法或多个单独方法 [英] ES6 modules: Export single class of static methods OR multiple individual methods

查看:833
本文介绍了ES6模块:导出单类静态方法或多个单独方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ECMAScript6模块。从以下选项中导出/导入多个方法的正确方法是什么?



单类静态方法:

  // ------ myClass.js ------ 

导出默认类myClass {

static myMethod1(){
console.log('foo');
}

static myMethod2(args ...){
console.log('bar');
}

}

// ------ app.js ------

import myClass from '我的课';
myClass.myMethod1(); // foo

多种导出方法:

  // ------ myMethods.js ------ 

导出函数myMethod1(){
console.log(' foo');
}

导出函数myMethod2(){
console.log('bar');
}

// ------ app.js ------
import {myMethod1,myMethod2} from'myMethods';
myMethod1()// foo;


// OR
import * as myMethods from'myMethods';
myMethods.myMethod1()// foo;

1)导出:
一类静态方法感觉像一个代码气味,但类似地导出一切个别感觉有点冗长。



2)导入:
'* as'语法是我首选的方法,因为它允许您使用点符号(引用模块AND方法),帮助代码可读性。

解决方案


A类的静态方法感觉就像一个代码气味


确实。这里不需要 class 结构!只需导出一个正常的模块对象:

  // ------ myMethods.js ------ 

export default {
myMethod1(){
console.log('foo');
},
myMethod2(args ...){
console.log('bar');
}
};




个别输出的内容有点冗长


好吧,你不需要任何包装器结构,所以我会说它更少的样板。


*您必须显式标记要导出的所有内容,这不是一件坏事。 as 语法是我的首选方法,因为它允许您使用点符号(引用模块和方法)帮助代码可读性。


这是非常个人喜好,并且取决于你编写的代码类型。有时简明是优越的,但是明确引用模块的能力也是有帮助的。注意,使用 * as 和默认导出对象的命名导出在这里非常相似,但只有命名导出允许您通过 import {myMethod1, myMethod2}


这是否有任何性能影响?




我不这么认为。



一般来说,静态标识符比属性访问更易于解析和优化 [1] ,多个命名导出和部分导入理论上可以使JIT更快,当然更小的文件需要更少的时间加载。



[1]:模块命名空间( import * as ns )也是静态的,即使 ns。... 看起来像一个动态属性访问


I'm using ECMAScript6 modules. What is the correct way to export/import multiple methods from a module from the options below?

Single class of static methods:

//------ myClass.js ------

export default class myClass {

  static myMethod1() {
    console.log('foo'); 
  }

  static myMethod2(args...) {
    console.log('bar'); 
  }  

}

//------ app.js ------

import myClass from 'myClass';
myClass.myMethod1();    //foo

Multiple exported methods:

//------ myMethods.js ------

export function myMethod1() {
    console.log('foo');
}

export function myMethod2() {
    console.log('bar');
}

//------ app.js ------
import {myMethod1, myMethod2} from 'myMethods';
myMethod1()    //foo;


//OR
import * as myMethods from 'myMethods';
myMethods.myMethod1()    //foo;

1) Exporting: A class of just static methods feels like a bit of a 'code smell' but similarly exporting everything individually does feel a bit verbose. Is it simply developer preference or are there performance implications here?

2) Importing: '* as' syntax is my preferred method as it allows you to use the dot notation (referencing both the module AND the method) aiding code readability. Does this have performance implications though when I may only be using 1 of the methods?

解决方案

A class of just static methods feels like a bit of a 'code smell'

Yes indeed. You don't need a class structure here! Just export a normal "module" object:

//------ myMethods.js ------

export default {
  myMethod1() {
    console.log('foo'); 
  },
  myMethod2(args...) {
    console.log('bar'); 
  }  
};

exporting everything individually does feel a bit verbose

Well, you don't need any wrapper structure, so I'd say it's less boilerplate. You just have to explicitly tag everything that you want to be exported, which is not a bad thing.

* as syntax is my preferred method as it allows you to use the dot notation (referencing both the module AND the method) aiding code readability.

That's very much personal preference, and does depend on the type of code you are writing. Sometimes conciseness is superior, but the ability to explicitly reference the module can be helpful as well. Notice that named exports using * as and default export objects are very similar here, though only named exports allow you to directly reference them via import {myMethod1, myMethod2}.

Does this have any performance implications?

I don't think so. Current ES6 implementations are not yet aiming for performance optimisations anyway.

In general, static identifiers are easier to resolve and optimise than property accesses[1], multiple named exports and partial imports could theoretically make JIT faster, and of course smaller files need less time to load. There hardly will be noticeable performance differences, you should use what is better maintainable.

[1]: module namespaces (import * as ns) are static as well, even if ns.… looks like a dynamic property access

这篇关于ES6模块:导出单类静态方法或多个单独方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆