I want to permit to call digest method only once, so function is deleted when digest has been calculated and that is a simple object without functions.
var Content = function(filename) {
var that = {};
var name = path.basename(filename);
var digest = function(callback) {
fs.readFile(filename, function(err, data) {
that.hash = crypto.createHash('sha1').update(data).digest('hex');
delete that.digest; // IS THIS SAFE?
callback();
});
};
that.digest = digest;
that.name = name;
return that;
};
Use it:
var content = Content('/path/to/file');
content.digest(function() {
// content.hash is available
});
Is this safe and good practice?