Javascript 如何使用附加到对象的方法创建npm包?
我正在尝试创建一个npm包,该包可能需要如下所示:Javascript 如何使用附加到对象的方法创建npm包?,javascript,node.js,npm,prototype,node-modules,Javascript,Node.js,Npm,Prototype,Node Modules,我正在尝试创建一个npm包,该包可能需要如下所示: var Greeks = require('greek-mythology-data'); var gods = new Greeks('gods'); gods.sortByName(); 但我只做到了以下几点: var Greeks = require('greek-mythology-data'); var gods = require('greek-mythology-data/data/gods.json'); gods = new
var Greeks = require('greek-mythology-data');
var gods = new Greeks('gods');
gods.sortByName();
但我只做到了以下几点:
var Greeks = require('greek-mythology-data');
var gods = require('greek-mythology-data/data/gods.json');
gods = new Greeks(gods);
console.log(gods.sortByName());
到目前为止,我有这个
var all = require('./data/greeks.json');
var demigods = require('./data/demigods.json');
var generalDeities = require('./data/general-deities.json');
var giants = require('./data/giants.json');
var gods = require('./data/gods.json');
var kings = require('./data/kings.json');
var primordialDeities = require('./data/general-deities.json');
var seaDeities = require('./data/sea-deities.json');
var titans = require('./data/titans.json');
function Greeks(collection) {
this.collection = collection;
/* return {
all: all,
demigods: demigods,
generalDeities: generalDeities,
giants: giants,
gods: gods,
kings: kings,
primordialDeities: primordialDeities,
seaDeities: seaDeities,
titans: titans,
}*/
}
Greeks.prototype.sortByName = function() {
var compare = function(a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
};
return this.collection.sort(compare);
};
module.exports = Greeks;
var all=require('./data/greenses.json');
var demigoods=require('./data/demigoods.json');
var generalDeities=require('./data/generalDeities.json');
var giants=require('./data/giants.json');
var goods=require('./data/goods.json');
var kings=require('./data/kings.json');
var primordialDeities=require('./data/general depties.json');
var seadenties=require('./data/seadenties.json');
var titans=require('./data/titans.json');
函数(集合){
this.collection=collection;
/*返回{
所有人:所有人,
半神:半神,
一般属性:一般属性,
巨人:巨人,
诸神,诸神,
国王:国王,
原始性:原始性,
海神:海神,
泰坦:泰坦,
}*/
}
greenses.prototype.sortByName=function(){
var比较=功能(a,b){
如果(a.nameb.name)
返回1;
返回0;
};
返回此.collection.sort(比较);
};
module.exports=希腊语;
我认为我现在的解决方案不是很方便
我是否采取了正确的方法
为了兼容性,我也在es5中编写了它,但我是否应该将其移动到ES6/ES2015并使用导入/导出和类?您可以使用常规的
模块。在具有函数的同一文件中导出类似的语法
module.exports = {
Greeks,
gods,
kings
// ... all other objects
}
然后像这样在主文件中使用它
var GreekMyth = require('greek-mythology-data');
var gods = new GreekMyth.Greeks(GreekMyth.gods);
console.log(gods.sortByName());
可以改进事物的命名您可以使用常规的模块。在具有函数的同一文件中导出类似的
语法
module.exports = {
Greeks,
gods,
kings
// ... all other objects
}
然后像这样在主文件中使用它
var GreekMyth = require('greek-mythology-data');
var gods = new GreekMyth.Greeks(GreekMyth.gods);
console.log(gods.sortByName());
事物的命名可以改进您似乎在寻找
const collections = {
all: all,
demigods: demigods,
generalDeities: generalDeities,
giants: giants,
gods: gods,
kings: kings,
primordialDeities: primordialDeities,
seaDeities: seaDeities,
titans: titans,
};
function Greeks(name) {
this.collection = collections[name];
}
尽管直接在模块接口上公开集合可能比用字符串引用它们更好。顺便说一句,在ES6中,您应该能够将上述内容简化为
import * as collections from './data';
你好像在找
const collections = {
all: all,
demigods: demigods,
generalDeities: generalDeities,
giants: giants,
gods: gods,
kings: kings,
primordialDeities: primordialDeities,
seaDeities: seaDeities,
titans: titans,
};
function Greeks(name) {
this.collection = collections[name];
}
尽管直接在模块接口上公开集合可能比用字符串引用它们更好。顺便说一句,在ES6中,您应该能够将上述内容简化为
import * as collections from './data';
我真的不知道你在问什么。看起来您已经有了一个可以满足您需要的工作解决方案。使用我的解决方案,我必须复制导入语句,您需要使用this.collection=require('./data/'+collection+'.json')
如果你想收藏
成为一条主线,我真的不知道你在问什么。看起来您已经有了一个可以满足您需要的工作解决方案。使用我的解决方案,我必须复制导入语句,您需要使用this.collection=require('./data/'+collection+'.json')
如果你想收藏
成为一个字符串,谢谢!我可以试试这个:)谢谢!我可以试试这个:)然后我会按照你的建议公开这些收藏。如果我使用es6,只有使用babel的“客户机”才能编译,对吗?你也可以将其传输到ES5/CommonJS并发布,然后我会按照你的建议公开这些集合。如果我使用es6,只有使用babel的“客户端”才能编译,对吗?您还可以将其传输到ES5/CommonJS并发布