使用嵌套类来模拟ES6中的命名空间 [英] Using nested classes to simulate namespaces in ES6

查看:108
本文介绍了使用嵌套类来模拟ES6中的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件, Services.js ,我试图加载我所有的个人服务。这些文件暴露为单身人士。



Services.js

  var Services = {}; 

导出默认服务;

然后我要将样例服务嵌套在服务下,所以我可以调用例如 Services.Sample.Operation()



`SampleService.js'

 从'./Services'导入服务; 

Services.Sample = {

操作:function(){
alert('operation!')
}
};

导出默认Services.Sample;

然后,我尝试导入:



<$从./services/Services导入服务的p $ p>
从'./services/SampleService'导入SampleService;新新200新新旗新新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗旗哨旗// yield'[object object]'
alert(SampleService); // yield'[object object]'
alert(Services.Sample); // yield'undefined'< ---这是我实际想要使用的

我可以得到它,所以我可以参考Services.Sample而不是tan SampleService。如何使SampleService嵌套在服务下?

解决方案

您的方式不起作用,因为您正在导入 Services.js SampleService.js 中,但服务变量不是服务变量从 Services.js



我会做的是这样的:



SampleService.js

  SampleService = {

操作:function(){
alert('operation!')
}
};

导出默认SampleService;

Services.js

 从'./SampleService'导入SampleService; 

var Services = {};
Services.Sample = SampleService;

导出默认服务;

然后:

从'./services/Services'导入服务的 

alert(服务);
alert(Services.Sample);

导出默认服务;

这对我的基本(依赖)和模块的一致性也似乎更有意义(定义服务可以在 Services.js SampleService.js SampleService 可以独立于服务,模块加载 Services.js 不应该依赖于 SampleService.js 以及以后可能会更改...)。


I have a file, Services.js which I am trying to load all of my individual services in. These are exposed as singletons.

Services.js

var Services = { };

export default Services;

I then want Sample Service nested under Services, so I could invoke for example Services.Sample.Operation()

`SampleService.js'

import Services from './Services';

Services.Sample = {

    Operation: function() {
        alert('operation!') 
    }
};

export default Services.Sample;

Then, I try and import:

import Services from './services/Services';
import SampleService from './services/SampleService';

alert(Services);         // yields '[object object]'
alert(SampleService);    // yields '[object object]'
alert(Services.Sample);  // yields 'undefined' <--- This is the one I actually want to use

How can I get it so I can refer to Services.Sample rather tan SampleService. How can I make SampleService become nested under Services?

解决方案

Your way doesn't work because you're importing Services.js in SampleService.js, but the Services variable is not the 'original' Services variable from Services.js.

What I'd do is something like this:

SampleService.js:

SampleService = {

    Operation: function() {
        alert('operation!') 
    }
};

export default SampleService;

Services.js:

import SampleService from './SampleService';

var Services = { };
Services.Sample = SampleService;

export default Services;

and then:

import Services from './services/Services';

alert(Services);         
alert(Services.Sample);

export default Services;

This does also seem to make more sense to me regarding basic (in)dependencies and consistency of your modules (define what Services can do in Services.js not SampleService.js, SampleService could be independent of Services, the module loading Services.js should not depend on SampleService.js as well as that might change later, ...).

这篇关于使用嵌套类来模拟ES6中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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