【发布时间】:2015-07-28 05:36:12
【问题描述】:
我正在尝试为我的 NodeJS 项目构建一组实用程序。这些助手将包括:文本实用程序(如子字符串、控制台日志记录等),以及更具体的助手,如解析推文的文本。
所以我试图将模块划分为不同的文件,并且非常清楚每件事的含义。
例如我想实现这个:
var helpers = require("helpers");
var Utils = new helpers.Utils();
// working with text
Utils.text.cleanText("blahblalh");
// working with a tweet
Utils.twitter.parseTweet(tweet);
如您所见,我通过调用非常具体的方法和子方法将 Utils 用于不同的事情。
我试图理解继承是如何在这里工作的,但我有点迷失了。
这就是我正在做的(一些粗略的示例代码):
//node_modules/helpers/index.js
var Text = require('./text');
var Twitter = require('./twitter');
function Utils() {
}
Utils.prototype.text = {
cleanText: function(text) {
Text.cleanText(text);
}
};
Utils.prototype.twitter = {
parseTweet(tweet) {
Twitter.parseTweet(tweet);
}
};
//node_modules/helpers/text.js
function Text() {
}
Text.prototype.cleanText = function(text) {
if (typeof text !== 'undefined') {
return text.replace(/(\r\n|\n|\r)/gm,"");
}
return null;
};
module.exports = Text;
//node_modules/helpers/twitter.js
function Twitter() {
};
Twitter.prototype.parseTweet = function(data) {
return data;
};
module.exports = Twitter
这是正确的方法吗?我是否做错了什么或者可能会减慢表演等?
【问题讨论】:
标签: javascript node.js