Answer the question
In order to leave comments, you need to log in
Javascript how to add function to function?
Good afternoon ladies and gentlemen.
I started writing a module for myself, for operating with a json file, my head does not understand how to implement the save function with blackjack and courtesans.
module.exports = function(filename, callback){
callback(filename)
}
module.exports.save = function(filename){
...fs...save()
}
Mdl('file.json', function(data){
data['blablabla'] = 12345;
}).save();
ffmpeg('/path/to/file.avi')
.videoCodec('libx264')
.audioCodec('libmp3lame')
.size('320x240')
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('end', function() {
console.log('Processing finished !');
})
.save('/path/to/output.mp4');
Mdl('file.json', function(data){
data['blablabla'] = 12345;
}).save();
module.exports = function(filename, callback){
this.data = callback(require(filename));
return {
save: function() {
//console.log(callback);
fs.writeFile(filename, data);
}
};
}
Answer the question
In order to leave comments, you need to log in
To do this, you just need to return an object from each function in the chain, in which there will be methods like save. The most simplified example:
function foo() {
return {
save: function() {
return true;
}
};
}
foo().save(); // true
class Foo {
constructor() {}
someMethod() {
return this;
}
anotherMethod() {
return this;
}
save() {
return this;
}
}
var obj = new Foo();
obj.someMethod().anotherMethod().save().someMethod().anotherMethod().save();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question