我需要将所有东西(函数和原型)从一个文件(name:config.js)导出到另一个将使用它们的文件(name:index.js)
我需要在“Sublime”上使用Node.js在Javascript中创建ws,
并且在cmd
'plus' in not defined
处运行我的代码时错误说明了。
code :(config.js)-first file
var Ranking=function(){
this.rank= 5;
events.EventEmitter.call(this);
};
var printName=function(){
console.log("spa name is: sea spa");
};
Ranking.prototype.plus =function(sum){
this.rank += sum;
this.emit("change");
};
Ranking.prototype.minus =function(sum){
this.rank -= sum;
this.emit("change");
};
var addingStar=function(){
console.log("the ranking is: " + this.rank);
};
var changeNoZero=function(){
if(this.rank<0){
console.log("bad reviews " + this.rank);
}
};
module.exports=Ranking;
index.js -second file
var events = require('events');
var util = require('util');
var Ranking = require('./config');
var ranking = new Ranking();
util.inherits(Ranking ,events.EventEmitter);
ranking.on("change",printName);
ranking.on("change",addingStar);
ranking.on("change",changeNoZero);
ranking.plus(1);
ranking.minus(6);
ranking.plus(3);
答案 0 :(得分:-1)
您可以尝试以下内容:
Config.js文件----
var defaultValues = module.exports
defaultValues.GetApiURL = function () {
var apiURL = "http://localhost:1000/api/"
return apiURL;
}
index.js ----
var defaultValues = require('./config.js');
var apiUrl= defaultValues.GetApiURL();