So, I first started .toString'ing a couple of validation functions, because that way I could use them both on the server side and the client side (awesome!). As I was just about to .toString() a function that is only used on the client side (but has to be send dynamically through ajax) I was re-evaluating this approach. For what it's worth it works splendidly in the current setup, but
.toString() an expensive function to run?.toString() will return something else than the source code?The only advantage in this case is that I can group certain related functions neatly together in a single class/file, but both in this case and the validation case it would also be possible to split it in a seperate .js file which I could require in node and serve to the client side directly for client use.
There is a huge disadvantage of using toString, you don't get any outer contexts of the function.
Something like this wouldn't work toStringed:
var validator = require('validator');
exports.validateEmail = function(email) {
return validator.isEmail(email) && ~email.indexOf('mydomain');
};
Tools such as browserify can compile server side node to work on the client with a single file without having to do any extra work.