I have the following code which is working OK. Now I need to add additional properties and functions and export them, How it's remended to do it? lets say I've two functions and two additional properties which I need to export.
properties like isValid prop1
var internal = require("../plu/internal");
module.exports = {isAvailable: false};
internal.eventEmitter.on('AppA', function () {
module.exports.isAvailable = true;
});
I have the following code which is working OK. Now I need to add additional properties and functions and export them, How it's remended to do it? lets say I've two functions and two additional properties which I need to export.
properties like isValid prop1
var internal = require("../plu/internal");
module.exports = {isAvailable: false};
internal.eventEmitter.on('AppA', function () {
module.exports.isAvailable = true;
});
Share
Improve this question
edited Jan 19, 2016 at 13:09
Louis
152k28 gold badges286 silver badges329 bronze badges
asked Jan 19, 2016 at 13:03
07_05_GuyT07_05_GuyT
2,88715 gold badges48 silver badges92 bronze badges
6
-
If you can have a default value on all, you could just do this
module.exports = {isAvailable: false, something: "Hello", somethingElse: "Hullo"};
and then have your event setters – magnudae Commented Jan 19, 2016 at 13:08 - Do you want to extend an existing class? Is this your intention? – Pho3nixHun Commented Jan 19, 2016 at 13:10
- Do you want to export setter and getter functions as properties of the exported object/module? – Endre Simo Commented Jan 19, 2016 at 13:11
- @SimoEndre correct this is what I need – 07_05_GuyT Commented Jan 19, 2016 at 13:20
- @magnudae - Thanks but how should I do it with the functions also can you provide example? – 07_05_GuyT Commented Jan 19, 2016 at 13:20
3 Answers
Reset to default 4The standard way to defined setter and getter methods on an object is to use the Object.defineProperty
method inside your internal
module. By exporting the internal
module you will export the setter
and getter
methods too.
Object.defineProperty(Internal.prototype, "isAvailable", {
get : function(){
return this.available;
},
set : function(value){
this.available = value;
}
});
or in ES6 you can write the same thing as:
get isAvailable() {
return this.available;
}
set isAvailable(value) {
this.available = value;
}
With Internal.prototype
you are extending the base object prototype with setter and getter methods.
This is an easy straight forward way. Correct me if I misunderstood. This will expose the getters and setters for everyone who requires this file. Unless you want to emit changes when they happen and only then this might not be the answer.
var available = false;
module.exports = {
getAvailable: getAvailable,
setAvailable: setAvailable
}
function getAvailable() { return available;}
function setAvailable(avail) { available = avail;}
Use below code, if it can solve your problem ?
var exports = module.exports = {};
var internal = require("../plu/internal");
exports.isAvailable = false;
internal.eventEmitter.on('AppA', function () {
exports.isAvailable = true;
});
exports.myTestFunc = function () {
return "Hello Test";
}
exports.testProperty = true;