简体   繁体   中英

Export Function and Prototype from one file to another

I need to export all things (function and prototype) from one file (name:config.js) to another file (name:index.js) that will use them I need to create ws in Javascript with Node.js on "Sublime", and the error said when in ran my code at cmd that '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);

You can try something as below :

Config.js file ----

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM